获取jwt(json web token)中存储的用户信息
一个JWT实际上就是一个字符串,它由三部分组成,头部(header)、载荷(Payload)与签名。
Payload
payload中可以保存用户的信息。
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")
};
获取所存放的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;
}
}