JWT授权鉴权(二)授权服务器端代码 C#

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);
此条目发表在C#, JWT分类目录。将固定链接加入收藏夹。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注