C# 生成JWT的Token

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;
        }
相关推荐
samble8 小时前
从零搭建灌装监控系统(十七):LiveCharts 实时温度曲线
c#·wpf·mvvm·modbus·工业控制
淡海水17 小时前
02-07-原理篇-GC暂停时间的本质
开发语言·unity·c#·gc·暂停时间
小羊没烦恼!18 小时前
OS X 上使用.NET开发应用程序
开发语言·前端·数据库·人工智能·c#·策略模式
Behavior20 小时前
Unity 7.0 Alpha 拆解:编辑器干掉 Mono 换 CoreCLR
c#·unity3d·游戏开发
淡海水20 小时前
02-06-原理篇-增量式GC与并发GC
算法·unity·c#·并发·gc·增量
Vertira21 小时前
C# 函数封装成 DLL,
开发语言·c#
浅尝辄止;1 天前
设计模式在HIS/LIS医疗系统中的完整实践指南(C#版)
开发语言·设计模式·c#
kimloner1 天前
开源一个 376KB 的 Windows 桌面小工具:便签 + 截图标注 + OCR + 待办提醒(C# / WinForms)
windows·开源·c#·ocr·winforms
阿松爱学习1 天前
【Unity开发】Aspose.PDF的使用
unity·pdf·c#
半亩码田1 天前
C#转Python第4.8篇:正则表达式:Python re 模块 vs C# System.Text.RegularExpressions
python·正则表达式·c#