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
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With