You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.5 KiB
C#
46 lines
1.5 KiB
C#
using EntrustSettle.Model.Dtos;
|
|
using System;
|
|
using System.IdentityModel.Tokens.Jwt;
|
|
using System.Security.Claims;
|
|
|
|
namespace EntrustSettle.AuthHelper
|
|
{
|
|
/// <summary>
|
|
/// JWTToken生成类
|
|
/// </summary>
|
|
public class JwtToken
|
|
{
|
|
/// <summary>
|
|
/// 获取基于JWT的Token
|
|
/// </summary>
|
|
/// <param name="claims">需要在登陆的时候配置</param>
|
|
/// <param name="permissionRequirement">在startup中定义的参数</param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
}
|
|
}
|