Click here to Skip to main content
11,569,265 members (58,717 online)
Click here to Skip to main content

Microsoft ASP.NET Identity MerlinFramework 2.0

, 28 Apr 2014 CPOL 21.6K 673 23
Rate this:
Please Sign up or sign in to vote.
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:

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.

 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.

 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)

Share

About the Author

Marc Leger
Web Developer Epsilon
United States United States
20+ years experience developing web solutions.

You may also be interested in...

Comments and Discussions

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

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150624.2 | Last Updated 28 Apr 2014
Article Copyright 2013 by Marc Leger
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid