Click here to Skip to main content
15,880,427 members
Articles / Database Development / SQL Server
Tip/Trick

Microsoft ASP.NET Identity MerlinFramework 2.0

Rate me:
Please Sign up or sign in to vote.
4.72/5 (7 votes)
28 Apr 2014CPOL1 min read 40.3K   23   14
Microsoft ASP.NET Identity MerlinFramework

Introduction

ASP.NET Identity is the new membership system for building ASP.NET web applications. ASP.NET Identity allows you to add login features to your application and makes it easy to customize data about the logged in user.

The following is a brief description of how I created my own membership system.

Background

When I created my first ASP.NET Web Application in Visual Studio 2013, I was prompted which Authentication to use. The options are:

  • No Authentication
  • Individual User Accounts
  • Organizational Accounts
  • Windows Authentication

By selecting Individual User Accounts, the site referenced Microsoft.AspNet.Identity.EntityFramework. I have yet to embrace EF. So, after a day or two researching, I came to the conclusion that I could either inherit from UserManager and override the virtual methods, or create new stores implementing the following interfaces:

C#
public class UserStore<TUser> : UserStore<TUser, 
IdentityRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>, IUserStore<TUser> 
  where TUser : IdentityUser
{
}
 
public class RoleStore<TKey, TRole> : RoleStore<TRole, string, 
IdentityUserRole>, IQueryableRoleStore<TRole> where TRole : IdentityRole, new()
{
}

Using the Code

To manage users, you simply instantiate UserManager located in the Microsoft.AspNet.Identity namespace, passing your store instance.

C#
var userStore = new UserStore<IdentityUser>();
var userManager = new UserManager<IdentityUser>(userStore);
var identityUser = new IdentityUser("mleger");
var result = userManager.CreateAsync(identityUser, "password");
Console.WriteLine("CreateAsync: " + result.Result.Succeeded);

To manage roles, you simply instantiate RoleManager located in the Microsoft.AspNet.Identity namespace, passing your store instance.

C#
var roleStore = new RoleStore<IdentityRole>();
var roleManager = new RoleManager<IdentityRole>(roleStore);
var identityRole = new IdentityRole("Administrator");
var result = roleManager.CreateAsync(identityRole);

The underlying database calls utilize Merlin.Data's DbManager located in Merlin Framework 2013. However, I've included the release versions of the DLLs.

Points of Interest

The purpose of this tip is to provide the source code that will hopefully assist you in creating your own, customized stores. The MVCWebExample project demonstrates the simplicity of replacing EntityFramework.

For more information on ASP.NET Identity, click here.

History

  • 06/17/2014 - Updated ZIP file
  • 05/11/2014 - Updated article
  • 04/27/2014 - Updated article for ASP.NET Identity 2.0
  • 11/09/2013 - Created

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer Epsilon
United States United States
20+ years experience developing web solutions.

Comments and Discussions

 
QuestionDivorce from Merlin Framework Pin
ZombieHorde15-Feb-18 9:30
ZombieHorde15-Feb-18 9:30 
QuestionMissing Source Code Pin
Michael K. Campbell16-Jun-14 17:26
Michael K. Campbell16-Jun-14 17:26 
AnswerRe: Missing Source Code Pin
Marc Leger17-Jun-14 13:14
Marc Leger17-Jun-14 13:14 
GeneralRe: Missing Source Code Pin
Michael K. Campbell18-Jun-14 6:12
Michael K. Campbell18-Jun-14 6:12 
Questionsource code file link broken Pin
Tridip Bhattacharjee28-Apr-14 20:54
professionalTridip Bhattacharjee28-Apr-14 20:54 
AnswerRe: source code file link broken Pin
Marc Leger29-Apr-14 4:28
Marc Leger29-Apr-14 4:28 
QuestionDitto on the Thanks Pin
Melodie deJong29-Dec-13 23:09
Melodie deJong29-Dec-13 23:09 
AnswerRe: Ditto on the Thanks Pin
Marc Leger30-Dec-13 6:50
Marc Leger30-Dec-13 6:50 
QuestionUser.Id as int Pin
ateista26-Nov-13 10:08
ateista26-Nov-13 10:08 
AnswerRe: User.Id as int Pin
Marc Leger2-Jan-14 5:46
Marc Leger2-Jan-14 5:46 
Bugsql injection! Pin
Spirch25-Nov-13 4:05
Spirch25-Nov-13 4:05 
GeneralRe: sql injection! Pin
Marc Leger25-Nov-13 15:49
Marc Leger25-Nov-13 15:49 
QuestionThank you Pin
Member 395592318-Nov-13 21:08
Member 395592318-Nov-13 21:08 
GeneralRe: Thank you Pin
Marc Leger19-Nov-13 4:11
Marc Leger19-Nov-13 4:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.