Custom MembershipProvider Initialize method
Asked Answered
A

3

9

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.

Achondrite answered 24/10, 2008 at 16:30 Comment(0)
A
21

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);
Achondrite answered 24/10, 2008 at 21:10 Comment(0)
B
7

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.

Booker answered 24/10, 2008 at 16:44 Comment(1)
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?Achondrite
H
0

In any case you shouldn't create instance of MembershipProvider. It is creating and initializating by standard asp.net infrastructure. You can access to it by code like this one:

var customerMembership = Membership.Provider;

Hallagan answered 27/1, 2014 at 1:13 Comment(1)
This worked for me! and it is the only way to ensure that your overrideable initialize function runs.Goodtempered

© 2022 - 2024 — McMap. All rights reserved.