从 Web 检索用户信息 API OAuth Bearer 身份验证

Retrieve user information from Web API OAuth Bearer authentication

我有一个使用令牌验证的可用 Web API,但我想检索发送该令牌的用户。

Request.GetOwinContext().Authentication.User.Identity.Name; // returns null

我怎样才能做到这一点?

我使用 owin 并以这种方式获取当前用户

ControllerContext.RequestContext.Principal.Identity.Name;

检查是否通过身份验证

ControllerContext.RequestContext.Principal.Identity.IsAuthenticated

确保用户名以这种方式链接到令牌(它在我的

SimpleAuthorizationServerProvider : OAuthAuthorizationServerProvider

class)

 ClaimsIdentity oAuthIdentity = new ClaimsIdentity(context.Options.AuthenticationType);
                oAuthIdentity.AddClaim(new Claim(ClaimTypes.Name, context.UserName));