【发布时间】:2019-06-22 16:29:23
【问题描述】:
我正在从 Postman 的标头中发送 Bearer Token。现在我需要使用该不记名令牌在 API 中获取用户声明。我尝试的代码不起作用意味着没有获取用户名/电子邮件。如何使用 Bearer Token 获取用户声明?
public class RepositoryUserAccount : IRepositoryUserAccount
{
private readonly HttpContext _httpContext;
public RepositoryUserAccount(IHttpContextAccessor httpContextAccessor)
{
this._httpContext = httpContextAccessor.HttpContext;
}
public async Task EnableAuthenticator()
{
ClaimsPrincipal currentUser = _httpContext.User;
var currentUserName = currentUser.FindFirst(ClaimTypes.NameIdentifier).Value;
}
}
【问题讨论】:
-
@RayanWilson 我的意思是没有得到像姓名/电子邮件这样的用户声明......
-
这是怎么注册的?单例还是传递?您的身份验证提供程序设置为什么?
-
@Daniel Singleton。身份验证提供程序设置????
-
_httpContext将为空或只是第一个请求命中。将其更改为在方法中使用访问器。 -
@Chandan Y S 您在登录用户时是否在令牌中设置用户声明?
标签: c# claims-based-identity httpcontext bearer-token