views:

225

answers:

2

Hi

I needed to rewrite lots of asp.net membership stuff since it did not meet my needs. So in my custom verify method I check their passwords if it fails. I grab the aspnet_membership maxInvalidPasswordAttempts column and add 1 to it.

So in my web.config I set it to 10. So I want to grab the value from the web.config and compare to what the user has. How can I do this?

Also I guess when I do this compare in the if statement I would lock the user out?

A: 

You should get the config values from web.config in your provider's Initialize method as shown below:

public override void Initialize(string name, NameValueCollection config)
    {
      if (config == null)
        throw new ArgumentNullException("config");

      // Initialize the abstract base class.
      base.Initialize(name, config);

      pApplicationName            = GetConfigValue(config["applicationName"], 
                                      System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath);
      pMaxInvalidPasswordAttempts = Convert.ToInt32(GetConfigValue(config["maxInvalidPasswordAttempts"], "5"));
      // ...

In your application, you would reference it as Membership.MaxInvalidPasswordAttmpts.

There is a full implementation of a custom MembershipProvider on MSDN. I was able to implement a provider within a couple hours using this as an example.

jrummell
A: 

From your application, you can access all of the providers associated with your application through the Providers collection on the Membership class. Then just use the property of the value you are interested in.

Membership.Providers["ProviderName"].MaxInvalidPasswordAttempts

Or you can get the default provider using the following.

Membership.Provider.MaxInvalidPasswordAttempts

Membership is in the System.Web.Security namespace, just in case.

Another approach though is probably to write your own MembershipProvider. Depending on how much customization you are going to be doing, your controllers will be cleaner if you handle all of your custom auth logic in your own provider.

Chuck
hmm I still don't get why I should make my own custom membership provider. Like since I am changing some stuff around could I just not trim the membership tables down and just sort of role my own solution out? Like if I have to override basically everything in the membership why make my own provider when I can just go and make my own tables with all the data I want or need then being limited to what was given to me? Like I like the membership stuff alot but it seems if you have alot of customization to do they making your own custom provider does not see worth it. Might as well make your own.
chobo2
Or am I missing something. So their is no easy way to get it from the web.config otherwise?
chobo2
I think I'm understanding your problem better now. You didn't write a provider or even wrap the MS provider, you just wrote your own entirely. In that case, ignore my answer.Creating a custom membership provider is still a good idea. It will work with the ASP.NET Configuration application launched from the Visual Studio - 'Projects' menu. They also allow other users to use your provider in a manner consistent with all the rest out there.If you have a really slimmed down functionality set, you may only need to implement 25%-50% of the methods.
Chuck