简体   繁体   English

更改.net成员资格ConnectionString

[英]Changing .net Membership ConnectionString

I need to change the connection string that the .net Membership API uses. 我需要更改.net Membership API使用的连接字符串。 Basically the API loads the connection string from app.config on the first time you are calling it. 基本上,API在您首次调用时从app.config加载连接字符串。

Does anyone knows how can I dynamically tell the API to use a different connection string? 有谁知道我如何动态地告诉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> 

EDIT: 编辑:

Solved - > Set connection string of membership dynamically from code 解决-> 通过代码动态设置成员资格的连接字符串

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: 您需要提供一个从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);
}

Then in your web.config, for the membership section, you need to put your new custom type in the 'type' attribute. 然后,在web.config中,对于“成员资格”部分,您需要将新的自定义类型放在“ type”属性中。 This requires you to get your new connection string from the ConnectionStrings application section. 这要求您从ConnectionStrings应用程序部分中获取新的连接字符串。

You may also need to also override the RoleProvider and the ProfileProvider depending on your requirements. 根据您的要求,您可能还需要覆盖RoleProvider和ProfileProvider。

PS Code quickly translated on the fly from VB.net, apologies if there are a few syntax errors. PS代码可以从VB.net快速快速地翻译出来,如果存在一些语法错误,我们深表歉意。


In the web.config, you must fully qualify your custom reference, something like (query from comments): 在web.config中,您必须完全限定您的自定义引用,例如(从注释中查询):

<add name="AspNetSqlMembershipProvider" 
type="zTester.tempMemebership, zTester, Version=1.0.0.0, Culture=neutral" ... etc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .Net framework 4.8 User Secret - 在成员资格提供者中访问 ConnectionString - .Net framework 4.8 User Secret - Accessing ConnectionString in Membership Provider 在Asp.Net Membership中手动更改用户名 - Manually changing username in Asp.Net Membership 更改asp.net成员资格的用户名 - Changing usernames in asp.net Membership 更改asp.net成员资格配置 - Changing asp.net membership configuration 如何获取成员资格提供程序正在使用的ConnectionString? - How to Get the ConnectionString that the Membership Provider is using? 通过将“userid”类型更改为整数来自定义asp.net成员资格 - customize asp.net membership by changing “userid” type to integer 更改自定义ASP.NET成员资格提供程序中的最小UserName长度? - Changing the minimum UserName length in a custom ASP.NET Membership Provider? 在Visual Studio 2012中更改asp.net成员表的目录 - changing directory of asp.net membership tables in visual studio 2012 在 ASP.NET MVC 3 中更改 Membership.ValidateUser() 和其他内容 - Changing Membership.ValidateUser() and other things in ASP.NET MVC 3 更改asp.net mvc3连接字符串后,asp.net配置不起作用 - after changing asp.net mvc3 connectionstring, asp.net configuration dont work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM