Tuesday, October 6, 2009

Creating a Custom Membership Provider

Here are the steps on how you can create your own membership provide in asp.net mvc framework:

1. First you need to create a class that inherits the abstract class MembershipProvider and implements all the abtract members.

public class CustomMembershipProvider : MembershipProvider

2. Override the function ValidateUser. Before you do this, I assume that you already created your database table.

 public override bool ValidateUser(string username, string password)
{
var user = (from u in _db.Users
where u.Username == username
&& u.Password == password
select u).SingleOrDefault();

if (user == null)
return false;
else
{
return true;
}
}


There are lot of procedures to override in the membership provider but I will let you do that with yourself.

3. Now, open your Web.Config and change the membership element to this:

<membership defaultprovider="CustomMembershipProvider">
<providers>
<clear/>
<add name="CustomMembershipProvider"
type="MVCApplication1.CustomMembershipProvider"
connectionStringName="ApplicationServices"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="true"
requiresUniqueEmail="false"
passwordFormat="Encrypted"
maxInvalidPasswordAttempts="5"
minRequiredPasswordLength="6"
minRequiredNonalphanumericCharacters="0"
passwordAttemptWindow="10"
passwordStrengthRegularExpression=""
applicationName="/" />
</providers>
</membership>


This are the settings for your membershipprovider.

THATS IT!
To test this you need to add dummy data to your users table.

No comments:

Post a Comment