MembershipProvider change connection string in code

久未见 提交于 2019-12-12 08:44:10

问题


I am trying to implement Asp.net Forms Authentication in my site. Usually, you provide the name of your DB connection string in your web.config. However, as our database setup is a little more complicated, I was looking for a way to manually provide the MembershipProvider the connection string in code.

Thanks!


回答1:


You don't have to use a connectionStringName for SqlMembershipProvider, instead you can supply the connection string directly. I.e. instead of:

<membership defaultProvider="SqlProvider" ...>
  <providers>
    <add name="SqlProvider" type="System.Web.Security.SqlMembershipProvider"
         connectionStringName="MyConnectionStringName" 
         .../>
  </providers>
</membership>

You can specify the connection string directly as:

<membership defaultProvider="SqlProvider" ...>
  <providers>
    <add name="SqlProvider" type="System.Web.Security.SqlMembershipProvider"
         connectionString="data source=... " 
         .../>
  </providers>
</membership>

Therefore you could also derive a custom provider from SqlMembershipProvider, and build the connection string dynamically as follows:

public class MySqlMembershipProvider : SqlMembershipProvider
{
    public override void Initialize(string name, NameValueCollection config)
    {
        config["connectionString"] = BuildMyCustomConnectionString();
        base.Initialize(name, config);
    }
}



回答2:


I came across this needing to do the same thing, set the connection string via code and not in the web.config, although I needed to change more than the name, I needed the actual value to be dynamically generated. If you want to change the actual connection string to be generated from code you can do the following:

web.config

...
  <connectionStrings>
    <add name="ConnectionPlaceHolder" connectionString="This is a place holder"/>
  </connectionStrings>
...
    <roleManager defaultProvider="SqlRoleProvider" enabled="true">
      <providers>
        <clear/>
        <add name="SqlRoleProvider" type="MyClassLibraries.Web.Security.MyCustomSqlRoleProvider" connectionStringName="ConnectionPlaceHolder" applicationName="MyApplicationName"/>
      </providers>
    </roleManager>

Provider Class

    public class MySqlRoleProvider : SqlRoleProvider
    {
        public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
        {
            try
            {
                config["connectionStringName"] = "ConnectionPlaceHolder";
                base.Initialize(name, config);

                FieldInfo connectionStringField = GetType().BaseType.GetField("_sqlConnectionString", BindingFlags.Instance | BindingFlags.NonPublic);
                connectionStringField.SetValue(this, ApplicationConfiguration.RetrieveApplicationConfigurationValue(myConnectionString));
            }
            catch (Exception ex)
            {
                CurrentOperation.RaiseException(ex);
                throw ex;
            }
        }

        private string myConnectionString()
        {
          return "Server=MyServer;database=MyDB;uid=aspnetDBUser;pwd=myPassword"
        }
    }

When you call base.Initialize() the .NET class requires there be a name specified in the web.config which is why you have to put something, so I just used a placeholder since I knew I would be overriding it in the code.

Our team did this because we needed to dynamically build connection strings based on different environments and didn't want to have to worry about having multiple web.configs floating around.



来源:https://stackoverflow.com/questions/16903988/membershipprovider-change-connection-string-in-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!