Pages

Wednesday, February 8, 2012

Implementing a Custom Membership Provider –MembershipProvider Control

  • Created a new Class called SQLMemebershipProvider:
  • Inherits from the abstract MembershipProvider class, which virtual methods of concern will be overridden.
  • Must reference: System.Web.Security, System.Collections.Specialized
  • Edit web.config and add within <system.web> tag
    <membership defaultProvider="SecurityTutorialsMembershipProvider">
      <providers>
        <clear/>
        <add name="SQLMembershipProvider"
             type="deDogs.Data.SqlMembershipProvider"
             connectionStringName="SecurityTutorialsConnectionString"
             enablePasswordRetrieval="false"
             enablePasswordReset="true"
             requiresQuestionAndAnswer="false"
             applicationName="SecurityTutorials"
             requiresUniqueEmail="true"
             passwordFormat="Hashed"
             maxInvalidPasswordAttempts="5"
             minRequiredPasswordLength="6"
             minRequiredNonalphanumericCharacters="1"
             passwordAttemptWindow="10"
             passwordStrengthRegularExpression=""
             />
      </providers>
    </membership>

The name, type, and connectionStringName tags from above are important.



  • Name is the name of the custom Membership Provider: SQLMembershipProvider
  • type is the class name, because Classes are types, so since I have placed the SQLMembershipProvider class within my custom namespaces (deDogs.Data), the type would be deDogs.Data.SQLMembershipProvider.
  • connectionStringName is the connection name of the SQL database created for membership schema (Creating The Membership Schema in SQL Server).

All attributes of the <add> tag are passed to the Initialize method of the MembershipProvider class. Passed in the collection parameter. Overridding the method the attribute values can be retrieved from the System.Collections.Specialized.NameValueCollection. The Initialize method:


I created one property above, which isn’t shown below in the code:



public override void Initialize(string name, NameValueCollection config)
{
    if (config == null)
    {
        throw new ArgumentNullException("config");
    }
    if (name == null || name.Length == 0)
    {
        name = "SQLMembershipProvider";
    }
    
    if (config["requiresQuestionAndAnswer"] == "true")
    {
        requiresQuestionAndAnswer = true;
    }
 
    base.Initialize(name, config);
}


The requiresQuestionAndAnswer attribute value is used to set the RequiresQuestionAndAnswer property. Must override readonly property:



public override readonly bool RequiresQuesionAndAnswe
{
    get
        {
            if (requiresQuestionAndAnswer)
                {
                    return true;
                }
                else
               {
                   return false;
               }
        }
}

Inheriting MembershipProvider abstract class, the class is populated with all methods that are overridden. They all return an NotImplementedException. Remove the exception.



public override bool RequiresQuestionAndAnswer

{
    get { throw new NotImplementedException(); }
}

and enter in the exception’s place:

No comments:

Post a Comment