【发布时间】:2018-02-28 20:05:20
【问题描述】:
我有一个使用令牌验证的有效 Web API,但我想检索发送该令牌的用户。
Request.GetOwinContext().Authentication.User.Identity.Name; // returns null
我怎样才能做到这一点?
【问题讨论】:
标签: c# asp.net-mvc oauth-2.0 asp.net-web-api2 owin
我有一个使用令牌验证的有效 Web API,但我想检索发送该令牌的用户。
Request.GetOwinContext().Authentication.User.Identity.Name; // returns null
我怎样才能做到这一点?
【问题讨论】:
标签: c# asp.net-mvc oauth-2.0 asp.net-web-api2 owin
我使用 owin 并以这种方式获取当前用户
ControllerContext.RequestContext.Principal.Identity.Name;
检查是否已通过身份验证
ControllerContext.RequestContext.Principal.Identity.IsAuthenticated
确保用户名以这种方式链接到令牌(它在我的
SimpleAuthorizationServerProvider : OAuthAuthorizationServerProvider
类)
ClaimsIdentity oAuthIdentity = new ClaimsIdentity(context.Options.AuthenticationType);
oAuthIdentity.AddClaim(new Claim(ClaimTypes.Name, context.UserName));
【讨论】: