Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting custom claim value from bearer token (Web API)

Tags:

In my ASP.NET Web API project I'm using bearer token authorization and I have added some custom claims to it, like this:

var authType = AuthConfig.OAuthOptions.AuthenticationType; var identity = new ClaimsIdentity(authType); identity.AddClaim(new Claim(ClaimTypes.Name, vm.Username));  // custom claim identity.AddClaim(new Claim("CompanyID", profile.CompanyId.ToString())); 

Is there any way I can access this additional claim value in the controller without an extra trip to the database?

like image 412
Impworks Avatar asked Oct 27 '14 12:10

Impworks


1 Answers

Sure, inside your protected controller you do the following:

 ClaimsPrincipal principal = Request.GetRequestContext().Principal as ClaimsPrincipal;  var customClaimValue = principal.Claims.Where(c => c.Type == "CompanyID").Single().Value; 
like image 145
Taiseer Joudeh Avatar answered Oct 17 '22 01:10

Taiseer Joudeh