Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / database / SQL-Server

Microsoft ASP.NET Identity MerlinFramework 2.0

4.72/5 (7 votes)
28 Apr 2014CPOL1 min read 42K   754  
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)