CurrentUser/ClaimsPrincipal

Phi*_*oyd 3 c# owin

我需要一些帮助.如何访问当前经过身份验证的用户/ ClaimsPrincipal.在过去,我会使用HttpContext.Current,但现在似乎有点不对劲.

Dan*_*ger 11

使用OWIN,可以从OWIN上下文的Authentication.User属性中找到用户.

OwinContext context = Request.GetOwinContext();
ClaimsPrinicipal user = context.Authentication.User;
// do stuff with user.
Run Code Online (Sandbox Code Playgroud)

Brock Allen有一篇关于在OWIN上使用cookie身份验证的帖子.

  • 如果您已添加到用户的配置文件中,如何使用所有配置文件信息访问User对象?或者另一种询问方式是你的方法得到一个'ClaimsPrincipal'对象.如何让'IdentityUser'对象完全水合?我知道我可以通过UserManager获取它,但我不希望每次需要用户时都调用该方法.这是唯一的方法吗? (2认同)