獲取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

相關文章
相關標籤/搜索