using EntrustSettle.Model.Dtos; using System; using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; namespace EntrustSettle.AuthHelper { /// /// JWTToken生成类 /// public class JwtToken { /// /// 获取基于JWT的Token /// /// 需要在登陆的时候配置 /// 在startup中定义的参数 /// public static TokenInfoViewModel BuildJwtToken(Claim[] claims, PermissionRequirement permissionRequirement) { var now = DateTime.Now; // 实例化JwtSecurityToken var jwt = new JwtSecurityToken( issuer: permissionRequirement.Issuer, audience: permissionRequirement.Audience, claims: claims, notBefore: now, expires: now.Add(permissionRequirement.Expiration), signingCredentials: permissionRequirement.SigningCredentials ); // 生成 Token var encodedJwt = new JwtSecurityTokenHandler().WriteToken(jwt); //打包返回前台 var responseJson = new TokenInfoViewModel { success = true, token = encodedJwt, expires_in = permissionRequirement.Expiration.TotalSeconds, token_type = "Bearer" }; return responseJson; } } }