Changing membership connection string

微笑、不失礼 提交于 2019-12-08 17:17:29

问题


Iam new to asp.net membership & I need help to change its connection string programmatically.

What I have tried till now is

I have create a class project name Sample as namespace** and extends the System.Web.Security.SqlMembershipProvider

code as

namespace Sample
{
    public class Connectionstring : SqlMembershipProvider
    {
        public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
        {
            string connectionString = "server=xx.xx.xx;database=db;user id=un;password=pwd";    

           // Set private property of Membership provider.  
           FieldInfo connectionStringField = GetType().BaseType
                     .GetField("_sqlConnectionString", BindingFlags.Instance |
                                                       BindingFlags.NonPublic);
           connectionStringField.SetValue(this, connectionString);
        }
    }
}

and altered web config file in membership tag as

<membership defaultProvider="SQLMembershipProvider">
  <providers>
    <add name="SQLMembershipProvider" type="sample.Connectionstring,sample" connectionStringName="SQLMembershipConnString" applicationName="@@@@@@@" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" passwordFormat="Hashed" />
  </providers>
</membership>

and while running the web application project the connection string which i am changing is not get altered?

waiting for your valuable responses and comments


回答1:


What i have also found on the net about problem is this here:

A simpler, albeit somewhat eyebrow-raising solution is just modifying the connection string in the providers early enough in the request's lifecycle:

     private void SetProviderConnectionString(string connectionString)
     {
         var connectionStringField = 
         Membership.Provider.GetType().GetField("_sqlConnectionString", 
                     BindingFlags.Instance | BindingFlags.NonPublic);

         if (connectionStringField != null)
             connectionStringField.SetValue(Membership.Provider, connectionString);
     }

Calling this method from Global.asax.cs inside Application_PreRequestHandlerExecute does the job. Haven't tested it too much, but even if something doesn't work, it just means it needs to be done earlier. No guarantees this will work with future versions of the framework, although most likely it will.

So, the 'SetProviderConnectionString' method can be called manually (after the Initialize method is finished), instead of expecting the framework to call the overwritten Initialize method when the Membership.Provider is referenced for a first time.




回答2:


For what reason are you not using a connection string in the web config?

Typically the web server will access the database using the web server's credentials, not an individual user's credentials.

This is a generic setup guide for asp.net authentication. http://vstudiojourney.blogspot.com/2008/06/choosing-another-database-for.html




回答3:


You don't appear to be calling base.Initialize from your Initialize override. I'm surprised anything works in this case.

In .NET 4 (not sure about earlier versions), you should be able to add the connection string to the configuration collection before calling base.Initialize as follows:

public override void Initialize(string name, NameValueCollection config)
{
    config["connectionString"] = ... whatever ...;
    base.Initialize(name, config);
}

This avoid the need to use reflection to access a private field.



来源:https://stackoverflow.com/questions/15265503/changing-membership-connection-string

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