views:

518

answers:

2

I derived a class from MembershipProvider in System.Web.Security with the following definition:

   public class RedditMembershipProvider : MembershipProvider
    {

        public RedditMembershipProvider()
        {

        }
        public override int PasswordAttemptWindow
        {
            get { return 10; }
        }
        /* snip all the similar properties definition
                 */
    }

The issue is I want when the RedditMembershipProvider is instantiated, I want all the properties to be read from the web.config, not from the code definition above:

<membership defaultProvider="RedditMembershipProvider">
<providers>
<clear/>
<add name="RedditMembershipProvider" 
     type="RedditClone.Models.RedditMembershipProvider"
     PasswordAttemptWindow=20
   />
</providers>
</membership>

So when I call the PasswordAttemptWindow from the View, I should get 20, not 10. But now I am getting 10, not 20.

Any idea how to solve this problem?

A: 

Silly me! How can I make this kind of mistake?

I just need to read web.config in my code to get the value out

  public override int PasswordAttemptWindow
    {
        get { ConfigurationManager.AppSettings["membership"].value; }
    }

An even easier way is to inherit the RedditMembershipProvider from SqlMembershipProvider and override only necessary details.

Ngu Soon Hui
+1  A: 
public class YourMembershipProvider : MembershipProvider
{
    public override void Initialize(string name, NameValueCollection config)
    {
        if (String.IsNullOrEmpty(config["yourValue"]))
        {
            _yourValue = config["yourValue"];
        }

        base.Initialize(name, config);
    }

    string  _yourValue;
    public override string YourValue
    {
        get
        {
            return _yourValue;
        }
    }

    ...
}
eu-ge-ne