Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Claim values in controller in MVC 5

I have used OWIN authentication in my application.

Login Action

var claims = new List<Claim>(); claims.Add(new Claim(ClaimTypes.Name, result.UserFirstName));             claims.Add(new Claim(ClaimTypes.Sid, result.UserID.ToString())); var identity = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie); 

I want to access the UserName and UserID from different action. How can I access the values which is added in the claims?

Update I have tried

var claims = new List<Claim>(); claims.Add(new Claim(ClaimTypes.Name, result.UserFirstName + " " + result.UserLastName));             claims.Add(new Claim(ClaimTypes.Sid, result.UserIDNumber.ToString())); var identity = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie); var authenticationManager = Request.GetOwinContext().Authentication; authenticationManager.SignIn(identity);  var claimsPrincipal = new ClaimsPrincipal(identity); Thread.CurrentPrincipal = claimsPrincipal; 

enter image description here

I can view the values inside the quick window. But even though I couldn't access the value. How to get the value?

like image 803
Golda Avatar asked Mar 07 '14 09:03

Golda


People also ask

What is ClaimsIdentity in MVC?

ClaimsIdentity(IIdentity) Initializes a new instance of the ClaimsIdentity class using the name and authentication type from the specified IIdentity. ClaimsIdentity(IIdentity, IEnumerable<Claim>) Initializes a new instance of the ClaimsIdentity class using the specified claims and the specified IIdentity.

What is ClaimsPrincipal?

ClaimsPrincipal exposes a collection of identities, each of which is a ClaimsIdentity. In the common case, this collection, which is accessed through the Identities property, will only have a single element.

What is Aspnetuserclaims?

A claims-based identity is the set of claims. A claim is a statement that an entity (a user or another application) makes about itself, it's just a claim. For example a claim list can have the user's name, user's e-mail, user's age, user's authorization for an action.


2 Answers

You need to set your Thread.CurrentPrincipal after login i.e.

var claims = new List<Claim>(); claims.Add(new Claim(ClaimTypes.Name, result.UserFirstName));             claims.Add(new Claim(ClaimTypes.Sid, result.UserID.ToString())); var identity = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie); var claimsPrincipal = new ClaimsPrincipal(identity); // Set current principal Thread.CurrentPrincipal = claimsPrincipal; 

Then the following will retrieve the values.

//Get the current claims principal var identity = (ClaimsPrincipal)Thread.CurrentPrincipal;  // Get the claims values var name = identity.Claims.Where(c => c.Type == ClaimTypes.Name)                    .Select(c => c.Value).SingleOrDefault(); var sid = identity.Claims.Where(c => c.Type == ClaimTypes.Sid)                    .Select(c => c.Value).SingleOrDefault(); 
like image 132
hutchonoid Avatar answered Oct 04 '22 17:10

hutchonoid


Here is another example, with custom claim types as well:

Login:

var claims = new List<Claim> {     new Claim(ClaimTypes.Name, user.Name, ClaimValueTypes.String),     new Claim(ClaimTypes.Email, user.Email ?? string.Empty, ClaimValueTypes.Email),     new Claim(ClaimTypes.PrimarySid, user.Id.ToString(), ClaimValueTypes.Integer),     new Claim(CustomClaimTypes.SalesId, user.SalesId.ToString(), ClaimValueTypes.Integer) };  var claimsIdentity = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie); AuthenticationManager.SignIn(claimsIdentity); 

Custom claims:

public static class CustomClaimTypes {     public const string SalesId = "SalesId"; } 

Extension methods:

public static class IdentityExtensions {     public static int GetSalesId(this IIdentity identity)     {         ClaimsIdentity claimsIdentity = identity as ClaimsIdentity;         Claim claim = claimsIdentity?.FindFirst(CustomClaimTypes.SalesId);          if (claim == null)             return 0;          return int.Parse(claim.Value);     }      public static string GetName(this IIdentity identity)     {         ClaimsIdentity claimsIdentity = identity as ClaimsIdentity;         Claim claim = claimsIdentity?.FindFirst(ClaimTypes.Name);          return claim?.Value ?? string.Empty;     } } 

Can then be accessed like this:

User.Identity.GetSalesId(); User.Identity.GetName(); 
like image 30
Ogglas Avatar answered Oct 04 '22 15:10

Ogglas