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

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 main thread:

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s