Click here to Skip to main content
Click here to Skip to main content

Microsoft ASP.NET Identity MerlinFramework 2.0

, 28 Apr 2014
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.

Comments and Discussions

 
QuestionMissing Source Code PinmemberMichael K. Campbell16-Jun-14 17:26 
AnswerRe: Missing Source Code PinmemberMarc Leger17-Jun-14 13:14 
GeneralRe: Missing Source Code PinmemberMichael K. Campbell18-Jun-14 6:12 
Questionsource code file link broken PinmemberTridip Bhattacharjee28-Apr-14 20:54 
AnswerRe: source code file link broken PinmemberMarc Leger29-Apr-14 4:28 
QuestionDitto on the Thanks PinmemberMelodie deJong29-Dec-13 23:09 
AnswerRe: Ditto on the Thanks PinmemberMarc Leger30-Dec-13 6:50 
QuestionUser.Id as int Pinmemberateista26-Nov-13 10:08 
AnswerRe: User.Id as int PinmemberMarc Leger2-Jan-14 5:46 
Bugsql injection! PinmemberSpirch25-Nov-13 4:05 
GeneralRe: sql injection! PinmemberMarc Leger25-Nov-13 15:49 
QuestionThank you PinmemberMember 395592318-Nov-13 21:08 
GeneralRe: Thank you PinmemberMarc 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 | Mobile
Web02 | 2.8.140827.1 | Last Updated 28 Apr 2014
Article Copyright 2013 by Marc Leger
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid