Search code examples
wifclaims-based-identityfederated-identity

Retrieving claims from a controller - how?


I'm told that if I want to grab a claim within a controller I can do something like:

IClaimsIdentity u = (IClaimsIdentity) this.HttpContext.User.Identity;
var ni = u.Claims.First(x => x.ClaimType == ClaimTypes.NameIdentifier).Value;

however, this violates the separation between views and controllers. the controller may be called in a context where there is no HttpContext - so what is the proper way to do it?

TIA - ekkis


Solution

  • Just leave out the HttpContext and use the User property of the controller directly:

    var u = (IClaimsIdentity)this.User.Identity;