Asp.net 更改.net成员资格连接字符串

Asp.net 更改.net成员资格连接字符串,asp.net,web-config,Asp.net,Web Config,我需要更改.net成员资格API使用的连接字符串。 基本上,API会在您第一次调用app.config时从app.config加载连接字符串 有人知道如何动态地告诉API使用不同的连接字符串吗 <system.web> <membership defaultProvider="SqlProvider"> <providers> <clear /> <add name="SqlProvider" type="System.W

我需要更改.net成员资格API使用的连接字符串。 基本上,API会在您第一次调用app.config时从app.config加载连接字符串

有人知道如何动态地告诉API使用不同的连接字符串吗

<system.web>
<membership defaultProvider="SqlProvider">
  <providers>
    <clear />
    <add name="SqlProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="MySqlConnection"
         enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
         passwordFormat="Hashed" maxInvalidPasswordAttempts="6545"
         minRequiredPasswordLength="4" minRequiredNonalphanumericCharacters="0"
         passwordAttemptWindow="10" passwordStrengthRegularExpression=""/>
  </providers>
</membership>
<roleManager enabled="true" defaultProvider="RoleProvider">
  <providers>
    <add name="RoleProvider"
         type="System.Web.Security.SqlRoleProvider"
         connectionStringName="MySqlConnection" />
  </providers>
</roleManager>
</system.web>
<connectionStrings>
  <add name="MySqlConnection"
       connectionString=".." 
       providerName="System.Data.SqlClient" />
</connectionStrings> 


您需要创建自己的自定义成员资格提供程序代码(这并不像听起来那么复杂)。您需要提供从SqlMembershipProvider继承的类,然后添加重写的初始化方法:

public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
{
    // Get your new connnection string name and then overwrite the base here:
    config.Item["connectionStringName"] = "MyNewConnectionStringName";

    base.Initilize(name, config);
}
然后在web.config中,对于成员资格部分,您需要将新的自定义类型放入“type”属性中。这要求您从ConnectionString应用程序部分获取新的连接字符串

根据您的要求,您可能还需要覆盖RoleProvider和ProfileProvider

从VB.net快速翻译的p.S.代码,如果出现一些语法错误,请道歉


在web.config中,您必须完全限定自定义引用,例如(从注释中查询):


创建了新类,但它无法识别配置中的属性'Item'或您从SqlMembershipProvider继承的'Initilize'。是的,添加了代码,我在文档中看到Item属性应该存在,我忘记了什么:/I我想我已经设法处理了这个属性,但现在它说:“无法从程序集'System.Web,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a'加载类型'zTester.TempMembershipProvider'。”我猜您在Web.config中配置该类型的方式不正确-您在配置部分有自己的命名空间吗?
<add name="AspNetSqlMembershipProvider" 
type="zTester.tempMemebership, zTester, Version=1.0.0.0, Culture=neutral" ... etc