Change membership connection string

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

I tried so far

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

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 the modified web configuration file in the participant 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 starting the web application project, the connection string that I am changing does not change?

waiting for your valuable feedback and comments

+7
source share
3 answers

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

Typically, the web server will access the database using the credentials of the web server, rather than the individual user credentials.

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

+1
source

What I found on the network about the problem is here :

A simpler, although somewhat eyebrow-raising solution simply modifies the connection string in providers early enough in the Life Cycle request:

  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. They have not tested too much, but even if something does not work, it just means that it needs to be done earlier. There are no guarantees that this will work with future versions of the structure, although, most likely, it will be.

Thus, the SetProviderConnectionString method can be called manually (after the completion of the Initialize method), instead of expecting the framework to call the rewritten Initialize method the first time it calls Memberhip.Provider.

+1
source

You do not call base.Initialize from your Override Initialize . I am surprised that in this case everything works.

In .NET 4 (not sure about earlier versions), you must 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 avoids the use of a reflex to access a private field.

+1
source

All Articles