Implement JWT authentication

删除回忆录丶 提交于 2019-12-11 07:39:56

问题


I am looking for the easiest way to implement JSON Web Token authentication using IdentityModel.Tokens.Jwt. Here is a link to a package itself:

JSON Web Token Handler For the Microsoft .Net Framework 4.5 4.0.1


回答1:


This is what worked for me:

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);


来源:https://stackoverflow.com/questions/27090547/implement-jwt-authentication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!