Custom ASP.NET SqlMembershipProvider - handling connection string

霸气de小男生 提交于 2019-12-05 18:56:00
Sky Sanders

ProviderBase will throw a ConfigurationException if there are any entries left in the config collection by the time it get's it so each provider removes it's configuration entries before calling base.Initialize.

The issue, as you have found as a result of this answer is that you must get your values before calling base.Initialize.

Sorry, I missed that at first glance.


The rest of this post is historical and while technically correct misses the salient issue here as enumerated above.

First - try WebConfigurationManager.ConnectionStrings.

WebConfigurationManager handles applying the hierarchy of web.config all the way from your windows\microsoft.net\framework\2.0xxxx\web.config all the way up to your app.

This behaviour is not present in ConfigurationManager, which typically deals with machine.config to app.config.


If this does not solve your problem you must be overwriting the value elsewhere in your code, if indeed _ConnectionStringName is being properly assigned in Initialize.

First, set a breakpoint and ensure that _ConnectionStringName is being set as expected.

Then locate all references to the field and ensure that you do not have a bug.

This is assuming, of course, that _ConnectionStringName is a private field. If it is not, make it so and look for your compile error.

Gregg

Not sure if this helps, but I was having a similar issue in needing to override the connectionstring in a sub-class of SqlMembershipProvider.

This idea is not my own - I found it in the comments section of this forum posting: http://forums.asp.net/p/997608/2209437.aspx

public override void Initialize(string name, NameValueCollection config)
{
     base.Initialize(name, config);<br>
     string connectionString =  //...what you want your connection string to be, 
                                //so config["connectionStringName"]...
     // Set private property of Membership provider.
     System.Reflection.FieldInfo connectionStringField = 
         GetType().BaseType.GetField("_sqlConnectionString", 
         System.Reflection.BindingFlags.Instance 
         | System.Reflection.BindingFlags.NonPublic);

     connectionStringField.SetValue(this, connectionString);
}

My apologies - I've never posted here before, so the formatting may be sub-par!

Chris S

This is probably 6 months too late to help, but I was able to get the connection string this way:

using System.Web.Configuration;

Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
MembershipSection section = config.SectionGroups["system.web"].Sections["membership"] as MembershipSection;
string defaultProvider = section.DefaultProvider;
string connstringName = section.Providers[defaultProvider].ElementInformation.Properties["connectionStringName"].Value.ToString();
string val = config.ConnectionStrings.ConnectionStrings[connstringName].ConnectionString;

This makes the assumption that the default provider has a connection string property - but if you're subclassing SqlMembershipProvider then should always have one, somewhere up the web.config chain (it's defined in the machine.config I believe).

All of this, to add one method to change the username.

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