mardi 8 mars 2016

Setting connection string dynamically

I have a web application and am trying to set the connection string used by membership and role providers dynamically. I searched and came across a few posts that suggested the same thing: create a generic connection string and have membership and role providers use it; set the connection string in global.asax. Something like this:

Web.config:

<connectionStrings>
  <add name="x" connectionString="content placeholder" />
</connectionStrings>

<roleManager enabled="true" defaultProvider="AspNetSqlRoleProvider">
  <providers>
    <remove name="AspNetSqlRoleProvider"/>
    <add name="AspNetSqlRoleProvider" 
      connectionStringName="x" applicationName="/"
        type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
  </providers>
</roleManager>

<membership defaultProvider="MyMembershipProvider">
  <providers>
    <clear />
    <add name="MyMembershipProvider"
      type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
      connectionStringName="x"
      applicationName="MyApp"
      enablePasswordRetrieval="false"
      enablePasswordReset="true"
      requiresQuestionAndAnswer="false"
      requiresUniqueEmail="false"
      minRequiredNonalphanumericCharacters="0"
      passwordFormat="Hashed" />
  </providers>
</membership>

Global.aspx:

private void SetProviderConnectionString(string connectionString)
{
  // Set private property of Membership, Role and Profile providers. 
  var connectionStringField = Membership.Provider.GetType().GetField("_sqlConnectionString", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
  if (connectionStringField != null)
    connectionStringField.SetValue(Membership.Provider, connectionString);

  var roleField = Roles.Provider.GetType().GetField("_sqlConnectionString", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
  if (roleField != null)
    roleField.SetValue(Roles.Provider, connectionString);

    var profileField = ProfileManager.Provider.GetType().GetField("_sqlConnectionString", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
    if (profileField != null)
      profileField.SetValue(ProfileManager.Provider, connectionString);
}

protected void Application_PreRequestHandlerExecute(object sender, EventArgs e)
{
  SetProviderConnectionString(Common.msSqlConn);
}

What I am stuck in is the parameter "_sqlCOnnectionString". I am using an Oracle 11g database. I tried adding the following to see if I can get a list FieldInfo but it returns an empty array:

System.Reflection.FieldInfo[] fi = Membership.Provider.GetType().GetFields();

Can someone tell me where "_sqlConnectionString" is supposed to come from or how do I modify this to use with Oracle membership/role providers?

My membership/role providers are of types:

type = "Oracle.Web.Security.OracleMembershipProvider, Oracle.Web, Version=2.112.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342"
type="Oracle.Web.Security.OracleRoleProvider, Oracle.Web, Version=2.112.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342".





Aucun commentaire:

Enregistrer un commentaire