csharp
复制代码
using JWT.Algorithms;
using JWT;
using JWT.Serializers;
private string GetToken(string timeStamp, string deptName, string doctorName, string idNo)
{
string token = string.Empty;
string appID = config.AppID;
string secretKey = config.AppSecret;
//十分钟有效期
long expTimeStamp = Convert.ToInt64(timeStamp) + 600000;
//载荷
var payload = new Dictionary<string, object>
{
{ "jti",config.AppID},
{ "name", config.AppName },
{ "orgName",config.HospitalName},
{ "deptName",deptName},
{ "doctorName",doctorName},
{ "idNo",idNo},
{ "idNoType","01"},
//生效时间
{ "nbf", timeStamp },
//过期时间
{ "exp", expTimeStamp.ToString() },
//颁发时间
{ "iat", timeStamp}
};
StringBuilder builder = new StringBuilder();
foreach (var value in payload)
{
builder.Append(value.Key);
builder.Append(":");
builder.Append(value.Value);
builder.Append(" ");
}
string logPayLoad = builder.ToString();
LoggerSimple.WriteInfo($"载荷:{logPayLoad}");
//HMACSHA256加密
IJwtAlgorithm algorithm = new HMACSHA256Algorithm();
//序列化和反序列
IJsonSerializer serializer = new JsonNetSerializer();
//Base64编解码
IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
IJwtEncoder encoder = new JwtEncoder(algorithm, serializer, urlEncoder);
//编码成JWT令牌
token = encoder.Encode(payload, secretKey);
LoggerSimple.WriteInfo($"生成token:{token}");
return token;
}