Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a ClaimsIdentity object for Asp.NET MVC 5?

I am working in a .NET MVC 5 application. I do not want to use Entity Framework. I want to authenticate to a RavenDB database. It looks to me that I want to replace the UserManager that comes with the Account Controller. I think I can rewrite all the UserManager functions to work with my database, except I don't understand the ClaimsIdentity object.

In the SignInAsync method, there is a call to UserManager.CreateIdentityAsync(...). I know it returns a ClaimsIdentity object. What I don't know is how to create a ClaimsIdentity object on my own.

I see that it has 4 properties Actor, BootstrapContext, Claims and Label. I don't know what these properties are used for, and I don't know how to properly generate them. I assume generating them correctly is important since it is how the authentication cookie is made.

I looked at the explanation of the ClaimsIdentity object here, but that didn't really help me understand.

If I could see the code for CreateIdentityAsync(), that would probably help.

If I'm going about this all wrong, please let me know. Otherwise, if someone could point me toward how to generate the ClaimsIdentity object, that would be helpful.

ClaimsIdentity identity = new ClaimsIdentity {     Actor = ????,     BootstrapContext = ?????,     Claims = ?????,     Label = ????? } 
like image 397
user1304444 Avatar asked Dec 05 '13 04:12

user1304444


People also ask

What is ClaimsIdentity in MVC?

The ClaimsIdentity class is a concrete implementation of a claims-based identity; that is, an identity described by a collection of claims.


Video Answer


2 Answers

Perhaps the following link can help:

var claims = new List<Claim>(); claims.Add(new Claim(ClaimTypes.Name, "Brock")); claims.Add(new Claim(ClaimTypes.Email, "[email protected]")); var id = new ClaimsIdentity(claims,DefaultAuthenticationTypes.ApplicationCookie);  var ctx = Request.GetOwinContext(); var authenticationManager = ctx.Authentication; authenticationManager.SignIn(id); 
like image 184
Vlince Avatar answered Sep 28 '22 22:09

Vlince


I think you are "going about this all wrong". The ASP.NET Identity Framework is designed with pluggable persistence in mind. The correct way to substitute RavenDB for EF is NOT to replace the UserManager. Rather, it is to implement a replacement UserStore. So the line in the AccountController that creates the UserManager would change from:

public AccountController()     : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()))) 

To:

public AccountController()     : this(new UserManager<ApplicationUser>(new RavenDBUserStore<ApplicationUser>/* connection info here*/))) 

Where the RavenDBUserStore would be a class you wrote that implemented IUserStore, IUserPasswordStore and whatever other *Store interfaces you needed for your particular application.

This approach avoids you needing to understand and re-implement everything in UserManager and ensures that you will be able to take advantage of future improvements to the UserManager from MS.

For more information on how to do this, see the Overview of Custom Storage Providers for ASP.NET Identity and the example of Implementing a Custom MySQL ASP.NET Identity Storage Provider. You should also check out the code of the RavenDB.AspNet.Identity Nuget Package created by David Boike mentioned in his answer. The source is on github at https://github.com/ILMServices/RavenDB.AspNet.Identity/tree/master/RavenDB.AspNet.Identity

like image 26
Jeff Walker Code Ranger Avatar answered Sep 28 '22 22:09

Jeff Walker Code Ranger