Token.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using Common.Config;
  2. using Common.Wechat;
  3. using CoreEntity.DAL;
  4. using CoreEntity.Entity;
  5. using Microsoft.Extensions.Caching.Memory;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Threading.Tasks;
  10. using ZcPeng.PublicLibrary;
  11. using ZcPeng.weixin.PublicAccount;
  12. namespace SupplierWeb.Codes.Auth
  13. {
  14. public class Token
  15. {
  16. private Guid _StaffId;
  17. private string _SignToken;
  18. private DateTime _ExpireTime;
  19. public Guid StaffId
  20. {
  21. get { return _StaffId; }
  22. set { _StaffId = value; }
  23. }
  24. public string SignToken
  25. {
  26. get { return _SignToken; }
  27. set { _SignToken = value; }
  28. }
  29. public DateTime ExpireTime
  30. {
  31. get { return _ExpireTime; }
  32. set { _ExpireTime = value; }
  33. }
  34. public static Token genAndSaveToken(IList<Account> users,IMemoryCache _cache,out string RoleName, AccessToken restoken = null)
  35. {
  36. var roleid = users[0].RoleId;
  37. var username = users[0].AccountRealName;
  38. //生成token
  39. #region 生成token
  40. if (roleid == 3)
  41. {
  42. //List<ContactDocExt> list = ContactDAL.getContactorByAccountId(users[0].Id.ToString());
  43. //if (list == null|| list.Count == 0) {
  44. // return Json(new { success = false, msg = "登陆错误,账户没有绑定公众号", user = new { id = "", username = username, password = password } });
  45. //}
  46. }
  47. Dictionary<string, Permission> permission = PermissionHelper.Permissions(users[0].RoleId);
  48. string results;
  49. object roleName = "";
  50. bool success = DataAccess.GetOneValue("select RoleRemark from " + Config.TablePrefix + "Role where id=" + roleid, out roleName, out results);
  51. RoleName = roleName ==null?"":roleName.ToString();
  52. var payload = new Dictionary<string, object>()
  53. {
  54. { "roleid", roleid },
  55. { "rolename", roleName },
  56. { "username", username },
  57. { "userid", users[0].Id },
  58. { "CurrentEmployeeID", users[0].PurStaffId },
  59. { "CurrentDeptID", "" },
  60. //{ "CurrentSupplierID", suppliersids },
  61. { "CurrentUserLastLoginTime", users[0].LastLoginTime },
  62. { "permission", permission },
  63. { "wxresponse", restoken }
  64. };
  65. var secretKey = TokenConfig.SecretKey;
  66. string SignToken = Jwt.JsonWebToken.Encode(payload, secretKey, Jwt.JwtHashAlgorithm.HS256);
  67. Token token = new Token();
  68. token.StaffId = Guid.NewGuid();
  69. token.SignToken = SignToken;
  70. token.ExpireTime = DateTime.Now.AddSeconds(TokenConfig.ExpireTime);
  71. _cache.GetOrCreate(token.StaffId.ToString(), entry =>
  72. {
  73. entry.SetAbsoluteExpiration(token.ExpireTime);
  74. return token.SignToken;
  75. });
  76. #endregion
  77. return token;
  78. }
  79. }
  80. }