实施 JWT 身份验证



我正在寻找使用IdentityModel.Tokens.Jwt实现JSON Web令牌身份验证的最简单方法。以下是包本身的链接:

适用于 Microsoft .Net Framework 4.5 4.0.1 的 JSON Web 令牌处理程序

这是对我有用的:

var securityKey = new InMemorySymmetricSecurityKey(Encoding.Default.GetBytes("MySecretKey"));
var header = new JwtHeader(new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256Signature, SecurityAlgorithms.Sha256Digest));
var payload = new JwtPayload();
var claims = new List<Claim>
{
    new Claim(ClaimTypes.Email, "jdoe@gmail.com"),
    ...
};
payload.AddClaims(claims);
// if you need something more complex than string/string data
payload.Add("tags", new List<string> { "admin", "user" });
var token = new JwtSecurityToken(header, payload);
var tokenString = securityTokenHandler.WriteToken(token);

相关内容

  • 没有找到相关文章