获取jwt(json web token)中存储的用户信息

一个JWT实际上就是一个字符串,它由三部分组成,头部(header)、载荷(Payload)与签名。ide

Payload

payload中能够保存用户的信息。ui

var claims = new Claim[]
{
new Claim(JwtRegisteredClaimNames.Sub, account),
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
new Claim(JwtRegisteredClaimNames.Iat, now.ToUniversalTime().ToString(),
ClaimValueTypes.Integer64),
//用户名
new Claim(ClaimTypes.Name,account),
//角色
new Claim(ClaimTypes.Role,"a")
};spa

获取所存放的account

var schemeProvider = context.RequestServices.GetService(typeof(IAuthenticationSchemeProvider)) as IAuthenticationSchemeProvider;
var defaultAuthenticate = await schemeProvider.GetDefaultAuthenticateSchemeAsync();
if (defaultAuthenticate != null)
{
var result = await context.AuthenticateAsync(defaultAuthenticate.Name);
var user = result?.Principal;
if (user != null)
{
account = user.Identity.Name;
}
}ip

相关文章
相关标签/搜索