开发者

Retrieving claims from a controller - how?

开发者 https://www.devze.com 2023-03-24 16:08 出处:网络
I\'m told that if I want to grab a claim within a controller I can do something like: IClaimsIdentity u = (IClaimsIdenti开发者_如何学Goty) this.HttpContext.User.Identity;

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

IClaimsIdentity u = (IClaimsIdenti开发者_如何学Goty) 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


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

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

精彩评论

暂无评论...
验证码 换一张
取 消