Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当前用户/索赔人_C#_Owin - Fatal编程技术网

C# 当前用户/索赔人

C# 当前用户/索赔人,c#,owin,C#,Owin,我需要一些帮助。如何访问当前经过身份验证的用户/ClaimsPrincipal。在过去,我会使用HttpContext.Current,但现在似乎有点错误。您想在什么上下文中访问它?我将从发布OAuth2令牌的Web API服务的角度来回答这个问题,因为这就是我刚才在程序中遇到的情况。如果这不是你的情况,请告诉我们你的具体情况 如果您想让经过身份验证的用户发出令牌,并且您已经覆盖了OAuthAuthorizationServerProvider,您可以在context.Request.user中

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

您想在什么上下文中访问它?我将从发布OAuth2令牌的Web API服务的角度来回答这个问题,因为这就是我刚才在程序中遇到的情况。如果这不是你的情况,请告诉我们你的具体情况

如果您想让经过身份验证的用户发出令牌,并且您已经覆盖了
OAuthAuthorizationServerProvider
,您可以在
context.Request.user
中找到它,其中
context
是被覆盖方法的参数


在继承自
ApiController
(在ASP.NET Web API中)的类中,可以检查
this.User
。这将是您放入OAuth2令牌中的
ClaimsPrincipal

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

OwinContext context = Request.GetOwinContext();
ClaimsPrinicipal user = context.Authentication.User;
// do stuff with user.

Brock Allen有一个。

如果您使用Web Api 2,您现在应该使用
RequestContext.Principal
来满足您的任何身份检查/分配需求。

如果您已添加到用户的配置文件中,您如何访问包含所有配置文件信息的用户对象?或者另一种询问方法是您的方法获取“ClaimsPrincipal”对象。如何使“IdentityUser”物体充分水合?我知道我可以通过UserManager获得它,但我不想每次需要用户时都调用该方法。这是唯一的办法吗?