动态设置连接字符串

时间:2016-03-08 13:38:01

标签: c# oracle reflection .net-3.5 connection-string

我有一个Web应用程序,我正在尝试动态设置成员资格和角色提供程序使用的连接字符串。我搜索并发现了一些建议相同的帖子:创建一个通用连接字符串并让成员资格和角色提供者使用它;在global.asax中设置连接字符串。像这样:

的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);
}

我遇到的是参数“_sqlCOnnectionString”。我正在使用Oracle 11g数据库。我尝试添加以下内容以查看是否可以获取列表FieldInfo但它返回一个空数组:

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

有人可以告诉我“_sqlConnectionString”应该来自哪里,或者我如何修改它以与Oracle会员/角色提供者一起使用?

我的会员/角色提供者属于以下类型:

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".

1 个答案:

答案 0 :(得分:0)

对于Oracle,你必须使用“m_OracleConnectionString”来代替_sqlConnectionString“。当我没有指定任何参数时,我在尝试获取字段信息时犯了一个错误。我使用了:

System.Reflection.FieldInfo[] fi = Membership.Provider.GetType().GetFields(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

,列表中的第一个参数是“mOracleConnectionString”。