In this tutorial I will show how to implement Custom Membership Provider in ASP.NET MVC 2 Web Application using Microsoft Visual Web Developer 2010 Express.
The tutorial will consist of few parts (not sure how many at this stage) and I will try to cover custom implementations of membership, role and profile providers.
I will use custom database schema and Entity Framework with additional repository class sitting between the Membership Provider and Entity Framework model.

Let’s start by creating a new Project called CustomMembership.

Because we’re using ASP.NET MVC 2 Web Application template, we can instantly run the application and get:

This default project is already configured to use ASPNETDB database (or to create one if does not exist) located in App_Data folder and standard ASP.NET Membership Provider . We want to use custom database schema to store membership information and to do that we will have to implement Custom Membership Provider. So let’s do it!

First, create new class called MyMembershipProvider in Models folder.

Remove the namespace definition and make your class inherit MembershipProvider class from System.Web.Security namespace.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;

public class MyMembershipProvider : MembershipProvider
{
}

Move MyMembershipProvider.cs file to App_Data folder by right clicking App_Data folder and adding an existing item, and remove it from Models folder by right clicking MyMembershipProvider.cs file and excluding it from the project.

Open App_Data\MyMembershipProvider.cs file for editing and right click on MembershipProvider class name and from the context menu choose Implement Abstract Class.

This will create override methods for MembershipProvider class.

You have just created Custom Membership Provider. None of the functionality is implemented yet but let’s just carry on and configure our application to use it.

Open Web.Config file in root of your project and change setting in membership node to use your new provider.

<membership defaultProvider="CustomMembershipProvider">
 <providers>
 <clear/>
 <add name="CustomMembershipProvider" type="MyMembershipProvider"
      connectionStringName="ApplicationServices"
      enablePasswordRetrieval="false"
      enablePasswordReset="true"
      requiresQuestionAndAnswer="false"
      requiresUniqueEmail="false"
      maxInvalidPasswordAttempts="5"
      minRequiredPasswordLength="6"
      minRequiredNonalphanumericCharacters="0"
      passwordAttemptWindow="10"
      applicationName="/" />
 </providers>
</membership>

If we run our project now, and try to log in using the link on the page you will see the following errors:

which is correct as we have not implemented any of the methods yet.
Let’s try to modify the ValidateUser method to return true and let us in.

public override bool ValidateUser(string username, string password)
{
 if (username == password)
 {
  return true;
 }
 else
 {
  return false;
 }
}

Now we should be able to log ourselves in as long as username and password are the same.
Let’s give it a go.

This is it for now! In the next part we will implement the database backend and UserRepository class.

Continue to:

ASP.NET MVC 2 Custom Membership Provider Tutorial – Part 2


Tags: , ,

21 Responses


  1. mark on 17 Nov 2010

    Thanks for the info! It works great. Please do part 2 soon!

    • LukeP on 19 Nov 2010

      Will definately post it this weekend.
      I’m glad someone finds it useful.

      Thanks

  2. Barinder on 19 Nov 2010

    Thanks! Was very helpful. Looking forward to part 2.

    • LukeP on 21 Nov 2010

      Right, part 2 is ready! Has taken me ages to write. Just need to go through all the steps myself to make sure everything is fine.
      I’ll post it later on tonight.

  3. Sebastian on 03 Feb 2011

    Great stuff, thanks man!

  4. Wojtek on 10 Feb 2011

    Hello There!
    I made everything following to the screenshots and found it working without implementing ValidateUser method. But while the first logging attempt i got an error like this:

    Parser Error Message: Could not load type ‘MyMembershipProvider’.

    Is there anything else I have to do to make it work fine? Maybe some entry in web.config?

    Regards,
    Wojtek

    • James on 31 Aug 2011

      I had the same problem.

      After removing the namespace from MyMembershipProvider.cs, it worked for me

      • zohreh on 16 Jul 2012

        thanks alot for your solution .I have this problem

  5. Wojtek on 10 Feb 2011

    Sorry to bother You, I already solved a problem.
    Thanks

  6. Satya on 23 Feb 2011

    I followed the exact procedure you mentioned.

    But when I tried opening the URL, an error occurred saying

    “Could not load type ‘MyMembershipProvider”

  7. Buffer on 16 May 2011

    Great series, but I’m puzzled why you are placing MyMembershipProvider.cs in the Models folder first, and then moving it to App_Data?

    • LukeP on 21 May 2011

      Hi. Thanks. It’s not an issue in MVC 3 but I couldn’t get it to work if I left MyMemberhipProvider in Models in MVC 2.

  8. Hoan on 24 Jun 2011

    I got this error message upon selecting logon:

    Parser Error Message: Could not load type ‘MyMembershipProvider’.

    Is there anything else in web.config file I need to modify?
    Please help.

    Thanks.

  9. Sri on 15 Sep 2011

    Hi Luke,

    First off, thank you for these detailed posts. For noobs like me, this is of great help!

    I am trying to follow your instructions and everything is fine until I get this error when I enter the username/password and attempt to logon.

    In AccountController.cs, the error comes up on this line:

    if (Membership.ValidateUser(model.UserName, model.Password))

    saying “Could not load type ‘LwMembershipProvider’. (path\web.config line 36)”

    LwMembershipProvider is the name I gave to the class.

    Line 36 in Web.Config is where the CustomMembershipProvider entry is, again, just as you have listed it above.

    I would greatly appreciate it if you could tell me where I was going wrong.

    Thanks a lot!
    Sri

  10. jimmy on 29 Sep 2011

    Getting Error Message: Could not load type ‘MyMembershipProvider’.

    Any ideas why?

  11. jimmy on 29 Sep 2011

    Figured that out. You need to include namespace name in the type also . So for example

  12. bot on 05 Jan 2012

    will this also works in mvc3?? thnx :)

  13. ashish on 05 Jan 2012

    getting error :
    AT Models/AccountModels.cs file error is Error 1 Cannot implicitly convert type ‘System.Web.Security.MembershipProvider’ to ‘MvcMembership.Models.MyMembershipProvider’. An explicit conversion exists (are you missing a cast?)

  14. eeer on 21 May 2012

    best membership article!
    I read it in 2 minutes and i get it.
    Thanks so much..

  15. zohreh on 16 Jul 2012

    getting error :
    Could not load type ‘MyMembershipProvider’. (C:\Users\1\Documents\Visual Studio 2010\Projects\CRM\CRM\web.config line 47)

  16. Mayur on 10 Sep 2012

    This is a very good example, thanks for sharing, however i have a question…

    I have an existing database with a users table, and we are planning to take the database and use it for a new system built in ASP.NET MVC. However, what I am uncertain about is whether or not I am able to create a login system that doesn’t use the built in account controller or a regular membership provider so that we can still use the existing table structure.

    So my question is, would this be possible? Or even particularly difficult to do if it is?

    What is the most widely accepted way of doing things and the simplest?


Leave your comment