Operator.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using JWT.Algorithms;
  2. using JWT.Builder;
  3. using JWT.Exceptions;
  4. using Org.BouncyCastle.Asn1.Pkcs;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using WMS.Util;
  11. namespace WMS.BZUtil
  12. {
  13. public interface IOperator
  14. {
  15. /// <summary>
  16. /// 生成jwt令牌
  17. /// </summary>
  18. /// <param name="userId">用户id</param>
  19. /// <param name="userName">用户名称</param>
  20. /// <param name="account">用户账号</param>
  21. /// <returns></returns>
  22. string EncodeToken(string userId, string userName, string account, string EncryptTokenNo, string warehouseNo);
  23. /// <summary>
  24. /// 解密jwt令牌
  25. /// </summary>
  26. /// <param name="token">令牌</param>
  27. /// <returns>TokenExpiredException 时间过期,SignatureVerificationException 签证不正确</returns>
  28. string DecodeToken(string token);
  29. }
  30. public class Operator : IOperator
  31. {
  32. /// <summary>
  33. /// 生成jwt令牌
  34. /// </summary>
  35. /// <param name="userId">用户id</param>
  36. /// <param name="userName">用户名称</param>
  37. /// <param name="account">用户账号</param>
  38. /// <returns></returns>
  39. public string EncodeToken(string userId, string userName, string account,string EncryptTokenNo,string warehouseNo)
  40. {
  41. var token = new JwtBuilder()
  42. .WithAlgorithm(new HMACSHA256Algorithm())
  43. .WithSecret(ConfigHelper.GetConfig().JwtSecret)
  44. .AddClaim("iat", DateTimeOffset.UtcNow.ToUnixTimeSeconds())
  45. .AddClaim("exp", DateTimeOffset.UtcNow.AddHours(ConfigHelper.GetConfig().JwtExp).ToUnixTimeSeconds()) // 设置12小时过期
  46. .AddClaim("UserId", userId).AddClaim("UserName", userName).AddClaim("Account", account).AddClaim("EncryptTokenNo", EncryptTokenNo).AddClaim("WarehouseNo", warehouseNo)
  47. .Encode();
  48. return token;
  49. }
  50. /// <summary>
  51. /// 解密jwt令牌
  52. /// </summary>
  53. /// <param name="token">令牌</param>
  54. /// <returns>TokenExpiredException 时间过期,SignatureVerificationException 签证不正确</returns>
  55. public string DecodeToken(string token)
  56. {
  57. try
  58. {
  59. var json = new JwtBuilder()
  60. .WithAlgorithm(new HMACSHA256Algorithm())
  61. .WithSecret(ConfigHelper.GetConfig().JwtSecret)
  62. .MustVerifySignature()
  63. .Decode(token);
  64. return json;
  65. //Console.WriteLine(json);
  66. }
  67. catch (TokenExpiredException)
  68. {
  69. return "TokenExpiredException";
  70. //Console.WriteLine("Token has expired");
  71. }
  72. catch (SignatureVerificationException)
  73. {
  74. return "SignatureVerificationException";
  75. //Console.WriteLine("Token has invalid signature");
  76. }
  77. }
  78. }
  79. /// <summary>
  80. /// 令牌中的信息
  81. /// </summary>
  82. public class Payload
  83. {
  84. /// <summary>
  85. /// 用户Id
  86. /// </summary>
  87. public string UserId { get; set; }
  88. /// <summary>
  89. /// 用户名称
  90. /// </summary>
  91. public string UserName { get; set; }
  92. /// <summary>
  93. /// 用户账号
  94. /// </summary>
  95. public string Account { get; set; }
  96. public string WarehouseNo { get; set; }
  97. public string EncryptTokenNo { get; set; }
  98. }
  99. }