Claim[] claims = new Claim[] {
new Claim(ClaimTypes.Name,data.Name),
new Claim("Id",data.Id.ToString()),
new Claim("UserName",data.UserName)
//不能放敏感信息
//此处便是(一)中的PAYLOAD的内容
};
//这个地方是授权服务器密匙的设置
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("SDMC-CJAS1-SAD-DFSFA-SADHJVF-VF"));
//这个地方是生成Token
var token = new JwtSecurityToken(
//授权服务器地址
issuer: "http://localhost:5059",
//鉴权服务器地址
audience: "http://localhost:5241",
claims: claims,
notBefore: DateTime.Now,
//此处是Token的过期时间
expires: DateTime.Now.AddHours(1),
signingCredentials: new SigningCredentials(key, SecurityAlgorithms.HmacSha256)
);
var jwtToken = new JwtSecurityTokenHandler().WriteToken(token);
return ApiResultHelper.Success(jwtToken);