更改站点的.NET成员的ConnectionString
我需要在.NET成员API使用更改连接字符串。
基本上API从app.config中你都称这是第一次加载的连接字符串。
I need to change the connection string that the .net Membership API uses. Basically the API loads the connection string from app.config on the first time you are calling it.
有谁知道我怎么能动态地告诉API来使用不同的连接字符串?
Does anyone knows how can I dynamically tell the API to use a different connection string?
<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>
编辑:
解决 - > Set动态地从code 会员的连接字符串
Solved - > Set connection string of membership dynamically from code
您需要创建自己的自定义成员资格提供code(这并不复杂,因为它的声音)。您需要提供从SqlMembershipProvider的继承类,然后添加一个重写的initialise方式:
You need to create your own custom membership provider code (this is not as complicated as it sounds). You need to provide a class that inherits from SqlMembershipProvider, and then add an overriden initialise method:
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文件,为会员节,你需要把你的新的自定义类型在类型属性。这需要你从该应用程序的ConnectionStrings的部分新的连接字符串。
Then in your web.config, for the membership section, you need to put your new custom type in the 'type' attribute. This requires you to get your new connection string from the ConnectionStrings application section.
您还可能还需要重写RoleProvider并根据您的要求ProfileProvider。
You may also need to also override the RoleProvider and the ProfileProvider depending on your requirements.
P.S。 code快译从VB.net飞,道歉,如果有几个语法错误。
P.S. Code quickly translated on the fly from VB.net, apologies if there are a few syntax errors.
在web.config中,你必须完全限定您的自定义参考,像(查询从评论):
In the web.config, you must fully qualify your custom reference, something like (query from comments):
<add name="AspNetSqlMembershipProvider"
type="zTester.tempMemebership, zTester, Version=1.0.0.0, Culture=neutral" ... etc