Updating connection string to LDAP with NHibernate and Oracle

Viewed 167

I've inherited an app that uses NHibernate and FluentNibernate to connect to an Oracle database. Unfortunately, I have no experience with NHibernate. The current connection string is like so:

Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=hostname)(PORT=0000)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=servicename)));User Id={username};Password={password};

but now needs to switch over to LDAP (see the last code snippet).

My question, is it possible to make a change only to the web.config with an LDAP connection or will I need to add the Oracle.ManagedDataAccess and make some code changes?

It is my understanding that OracleClientConfiguration has been deprecated based on this page hence the part of the question about a new nuget package. Another note, there isn't a hibernate.cfg.xml file within the project (which I think is ok?) but wondering if this project isn't using the best practices for NHibernate. Any other hints or tips would be appreciated.

C# code:

private ISessionFactory GetSessionFactory()
{
      var connectionString = LoginHelper.GetConnectionString(ConnectionStringSetting, LoginInfoSetting);

      var iSessionFactory
         = Fluently
            .Configure()
            .Database(OracleClientConfiguration.Oracle10.ConnectionString(connectionString))
            .Mappings(e => e.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly()))
            .BuildSessionFactory();

      return iSessionFactory;
}

Web.config (there no other references to NHibernate in the Web.config. There are some references to dependentAssembly in other dll.config files):

<configuration>
   <configSections>
      <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
   <configSections>
<configuration>

What I (think I) need in the Web.config is something like so:

<oracle.dataaccess.client> <!-- or <oracle.manageddataaccess.client>? -->
  <version number="*">
      <LDAPsettings>
         <LDAPsetting name="DIRECTORY_SERVERS" value="" />
         <LDAPsetting name="DIRECTORY_SERVER_TYPE" value="" />
         <LDAPsetting name="DEFAULT_ADMIN_CONTEXT" value="" />
      </LDAPsettings>
      <settings>
         <setting name="NAMES.DIRECTORY_PATH" value="" />
         <setting name="LDAP_ADMIN" value="" />
      </settings>
   </version>
</oracle.dataaccess.client> 

EDIT 1: Contents of ldap.ora file:

DIRECTORY_SERVERS=(ldap:XXX)
DEFAULT_ADMIN_CONTEXT="dc=Oracle,dc=com"
DIRECTORY_SERVER_TYPE=ad
2 Answers

Here is what I did in order to get this to work...

In the Oracle folder structure, navigate to the Network folder.

Add the following files (should be able to copy and paste another folder down called Sample):

  • sqlnet.ora
  • ldap.ora
  • tnsnames.ora

sqlnet.ora (the key here was adding LDAP to the beginning of the list):

SQLNET.AUTHENTICATION_SERVICES= (NTS)
NAMES.DIRECTORY_PATH= (LDAP, TNSNAMES, EZCONNECT)

ldap.ora:

DIRECTORY_SERVERS=(ldap:XXX:XXX)
DEFAULT_ADMIN_CONTEXT="dc=oracle,dc=com"
DIRECTORY_SERVER_TYPE=XX

tnsnames.ora:

No change was required; left as default

Connection string:

<add name="connection" connectionString="Data Source=YourDataSource"/>
<!-- might need to add a username/password -->

According documentation it should be this:

<LDAPsettings>
  <LDAPsetting name="DIRECTORY_TYPE" value="AD" />
  <LDAPsetting name="DEFAULT_ADMIN_CONTEXT" value="dc=Oracle,dc=com"/>
</LDAPsettings>

Or use

<settings>
  <setting name="TNS_ADMIN" value="C:\oracle\work"/>
</settings>

and specify LDAP settings in C:\oracle\work\ldap.ora file (in conjunction with C:\oracle\work\sqlnet.ora file).

I don't think it is still valid but have a look at ODP.NET Managed library does resolve alias, but 32-bit library does

You can modify the config file with a simple text editor or use the config tool OraProvCfg.exe. Would be like this:

OraProvCfg.exe /action:config /product:odpm /frameworkversion:v4.0.30319 /providerpath:C:\oracle\product\12.1\Client_x64\odp.net\managed\common\Oracle.ManagedDataAccess.dll /set:settings\TNS_ADMIN:C:\oracle\network\admin

OraProvCfg.exe /action:config /product:odpm /frameworkversion:v4.0.30319 /providerpath:C:\oracle\product\12.1\Client_x64\odp.net\managed\common\Oracle.ManagedDataAccess.dll /set:LDAPsettings\DIRECTORY_TYPE:ad

Note, you have a OraProvCfg.exe for 32-bit (x86) and for 64-bit. Run the one which matches to your application, (or simply run both).

Related