123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- using JWT.Algorithms;
- using JWT.Builder;
- using JWT.Exceptions;
- using Org.BouncyCastle.Asn1.Pkcs;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using WMS.Util;
- namespace WMS.BZUtil
- {
- public interface IOperator
- {
- /// <summary>
- /// 生成jwt令牌
- /// </summary>
- /// <param name="userId">用户id</param>
- /// <param name="userName">用户名称</param>
- /// <param name="account">用户账号</param>
- /// <returns></returns>
- string EncodeToken(string userId, string userName, string account, string EncryptTokenNo, string warehouseNo);
- /// <summary>
- /// 解密jwt令牌
- /// </summary>
- /// <param name="token">令牌</param>
- /// <returns>TokenExpiredException 时间过期,SignatureVerificationException 签证不正确</returns>
- string DecodeToken(string token);
- }
- public class Operator : IOperator
- {
- /// <summary>
- /// 生成jwt令牌
- /// </summary>
- /// <param name="userId">用户id</param>
- /// <param name="userName">用户名称</param>
- /// <param name="account">用户账号</param>
- /// <returns></returns>
- public string EncodeToken(string userId, string userName, string account,string EncryptTokenNo,string warehouseNo)
- {
- var token = new JwtBuilder()
- .WithAlgorithm(new HMACSHA256Algorithm())
- .WithSecret(ConfigHelper.GetConfig().JwtSecret)
- .AddClaim("iat", DateTimeOffset.UtcNow.ToUnixTimeSeconds())
- .AddClaim("exp", DateTimeOffset.UtcNow.AddHours(ConfigHelper.GetConfig().JwtExp).ToUnixTimeSeconds()) // 设置12小时过期
- .AddClaim("UserId", userId).AddClaim("UserName", userName).AddClaim("Account", account).AddClaim("EncryptTokenNo", EncryptTokenNo).AddClaim("WarehouseNo", warehouseNo)
- .Encode();
- return token;
- }
- /// <summary>
- /// 解密jwt令牌
- /// </summary>
- /// <param name="token">令牌</param>
- /// <returns>TokenExpiredException 时间过期,SignatureVerificationException 签证不正确</returns>
- public string DecodeToken(string token)
- {
- try
- {
- var json = new JwtBuilder()
- .WithAlgorithm(new HMACSHA256Algorithm())
- .WithSecret(ConfigHelper.GetConfig().JwtSecret)
- .MustVerifySignature()
- .Decode(token);
- return json;
- //Console.WriteLine(json);
- }
- catch (TokenExpiredException)
- {
- return "TokenExpiredException";
- //Console.WriteLine("Token has expired");
- }
- catch (SignatureVerificationException)
- {
- return "SignatureVerificationException";
- //Console.WriteLine("Token has invalid signature");
- }
- }
- }
- /// <summary>
- /// 令牌中的信息
- /// </summary>
- public class Payload
- {
- /// <summary>
- /// 用户Id
- /// </summary>
- public string UserId { get; set; }
- /// <summary>
- /// 用户名称
- /// </summary>
- public string UserName { get; set; }
- /// <summary>
- /// 用户账号
- /// </summary>
- public string Account { get; set; }
- public string WarehouseNo { get; set; }
- public string EncryptTokenNo { get; set; }
- }
- }
|