Custom Membership Provider with Form Authentication in ASP.Net MVC Application


In this article I will demonstrate in four simple steps from scratch - How to create Custom Membership Provider in ASP.Net MVC Application for Form Authentication.

It would serve as a replacement for the ASP.Net's built in membership provider when you feel that built in membership provider are not appropriate for your application.

  1. Start by Creating a new Visual Studio Solution. Open a new instance of Visual Studio 2010.

    Select File -> New -> Project and from the new project dialog select ASP.Net MVC 2 Web Application.

    CusMemMVC1.gif

    Click" Ok"- for this sample application – Select" No, do not create a unit test Project.
     
  2. From the Solution explorer – Go to Global.asax.cs - Update RegisterRoutes() method to make Account Controller – LogOn – the default one – so that when we run our application -LogOn View gets called instead of redirecting it to Home – Index

    Inside RegisterRoutes() replace
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } with
    new { controller = "Account", action = "LogOn", id = UrlParameter.Optional }
     
  3. Next Create- Custom Membership Provider by deriving a class from MembershipProvider

    • Go to Solution Explorer – Models – Right Click – Add Class- CustomMembershipProvider.cs

    • Add the following using statement to CustomMembershipProvider.cs

           
    using System.Web.Security;

    • Derive CustomMembershipProvider from class – MembershipProvider

          
    public class CustomMembershipProvider:MembershipProvider
         {
     
         }


    • Right-click MembershipProvider and choose Implement Abstract Class

    CusMemMVC2.gif

    • You will find a lot of properties and methods - all throwing a NotImplementedException

    • For Form Authentication only method we need to update is ValidateUser().

    Add your logic here to validate the Usernames and password.

    public override bool ValidateUser(string username, string password) 
    {
     if (username == "saurabh" && password == "singh")
            {
                  return true;
           }

    else
           {
                  return false;
           }
     
    }

    For this sample I have hardcoded the username and password in the code.
    In the actual scenario - fetch the username and password from the corresponding table in database.
     

  4. After creating the custom membership provider , You need to register it in the Web.Config File
    Replace the default membership with the following section

     <configuration>

      <system.web>

           <membership defaultProvider="MyMembershipProvider">
                  <providers>
                  <clear/>
    <add name="MyMembershipProvider"                                     type=" CutomMemberShipProvider.Models.CustomMembershipProvider"/>
                  </providers>
           </membership>

    </system.web>
    </configuration>

     

  5. Compile and run the Project (F5) and you will get directed to Log On Page.

    You can login with the logic provided in the ValidateUser() method.
    For this sample application since we have hardcoded user name and password , So login using User name=saurabh and password=singh for successful Authentication.

    CusMemMVC3.gif

In the above article we have seen how to implement Custom Membership Provider. I have attached the Code for this sample Application.

To keep the article simple I have used the ready made Account Controller, LogOn View, and Account Model coming with the default MVC project template. All these files can be modified to meet the project specific requirements.

erver'>

Similar Articles