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;
        }
相关推荐
国家一级摸鱼选手4 小时前
[Unity]几何碰撞检测:SAT 分离轴定理详解(从数学原理到 C# 实现)
unity·c#·游戏引擎·分离轴定理(sat)·多边形相交
汤姆宝库17 小时前
BPE分词器的完整实现:字节对合并与词表构建
开发语言·人工智能·c#
Risehuxyc1 天前
C# 发展历史
开发语言·c#
kyle~1 天前
C# --- 反射
开发语言·c#
暴走约伯2 天前
【VFX工具链源码拆解 09】终极工业化闭环:基于 ID 规则驱动的全自动场景建档、骨骼挂载与男转女场景迁移
unity·c#·游戏引擎·材质
用户788477316343 天前
WinForms 迁移 WPF:Upgrade Assistant 覆盖 30%,剩下 70% 我手写了六周
c#
寂寞的船3 天前
【Unity知识速递】第13期 Unity 内存泄漏排查实战:从 Memory Profiler 到 Lua 引用泄漏的生产级全链路诊断
unity·junit·c#·游戏引擎·lua
用户3721574261354 天前
C# 提取 PDF 附件:包括文档附件和页面注释附件
c#
寂寞的船4 天前
【Unity Bug挑战】第12期 Awaitable 二次 await 死锁之谜 —— 为什么同一个异步操作等两次就卡死?
unity·c#·游戏引擎·bug
huaqianzkh5 天前
WinForm C#:循环修改控件UI闪烁卡顿?SuspendLayout / ResumeLayout 与 BeginUpdate 优化
开发语言·ui·c#