views:

1458

answers:

2

When overriding the MembershipProvider and calling it directly, is there a way to fill the NameValueCollection config parameter of the Initialize method without manually looking through the config file for the settings?

Obviously this Initialize is being called by asp.net and the config is being filled somewhere. I have implemented my own MembershipProvider and it works fine through the build in controls. I would like to create a new instance of my provider and make a call to it directly, but I don't really want to parse the .config for the MembershipProvider, it's connection string name and then the connection string if it's already being done somewhere.

A: 

Not sure why you want to create a new one, but if you create it yourself, you'll need to read the web config and get the values yourself to pass to Initialize() as this is done outside the class. I'm sure, though, that there is already a section handler for this section so it should be just a matter of doing:

MembershipSection section  = WebConfigurationManager.GetSection("membership");

Then find your provider and accessing its properties to construct the NameValueCollection. I don't think you will have to write any code to parse the configuration section.

Here is the MembershipSection documentation at MSDN. Drill down from there.

tvanfosson
I tried this and section is null. I looked at the WebConfigurationManager and all I have is AppSettings and ConnectionStrings. Is there a reason membership isn't showing up? I see it in the web.config?
JHORN
+6  A: 

tvanfosson- Thanks for the help. (if I had the 15 points necessary I would vote you up)

From your link I was able to figure it out. It turns out the second parameter to the Initialize proceedure was the list of parameters from the provider and could be reached in the following way:

string configPath = "~/web.config";
Configuration config = WebConfigurationManager.OpenWebConfiguration(configPath);
MembershipSection section = (MembershipSection)config.GetSection("system.web/membership");
ProviderSettingsCollection settings = section.Providers;
NameValueCollection membershipParams = settings[section.DefaultProvider].Parameters;
Initialize(section.DefaultProvider, membershipParams);
JHORN
@JHORN: you now have those 15 necessary points :)
Abel