如何从 .netcore 中的令牌中删除"http://schemas.xmlsoap.org/ws/2005/05/identity/ URL"



这是我的令牌生成代码:

var claims = new List<Claim>
{
new Claim(ClaimTypes.NameIdentifier, appUser.Id.ToString()),
new Claim(ClaimTypes.Name, appUser.UserName)
};
var roles = await _userManager.GetRolesAsync(user);
foreach (var role in roles)
{
claims.Add(new Claim(ClaimTypes.Role, role));
}
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config.GetSection("AppSettings:Token").Value));
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha512Signature);
string issuer = _config["Token:Issuer"];
var jwtSecurityToken = new JwtSecurityToken(
issuer: issuer,
audience:issuer,
claims: claims,
expires: DateTime.Now.AddMinutes(5),
signingCredentials: new SigningCredentials(key, SecurityAlgorithms.HmacSha256)
);
jwtSecurityToken.Header.Add("kid", requestAPIKey);
var token = new JwtSecurityTokenHandler().WriteToken(jwtSecurityToken);

令牌输出如下:

{
"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier": "7",
"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name": "ligeros",
"exp": 1540619440,
"iss": "http://localhost:5000",
"aud": "http://localhost:5000"
}

我如何才能将其输出?

{
"nameid": "7",
"unique_name": "ligeros",
"nbf": 1540613190,
"exp": 1540703190,
"iat": 1540613190
}

我不在乎nbf,exp和iat。我只希望URL被删除,变成nameid,unique_name,而不是http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier和http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name。非常感谢。

我修复了它:

var claims = new List<Claim>
{
new Claim("nameid", appUser.Id.ToString()),
new Claim("unique_name", appUser.UserName)
};
var roles = await _userManager.GetRolesAsync(user);
foreach (var role in roles)
{
claims.Add(new Claim("roles", role));
}

如果你仍然找不到它。你应该使用jwtsecuritytokenhandler.createjwttoken((方法。使用新jwtsecuritytoken。有关更多信息和详细信息,请查看我的问题。

相关内容

最新更新