SsoTokenHelper.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. using Common.Config;
  2. using Common.Http;
  3. using Microsoft.AspNetCore.Http;
  4. using Microsoft.Extensions.Caching.Memory;
  5. using Microsoft.Extensions.Primitives;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Web;
  11. //using Liger.Common;
  12. namespace LigerRM.Common
  13. {
  14. /// <summary>
  15. /// 不支持 cookie values
  16. /// </summary>
  17. public class SsoTokenHelper
  18. {
  19. public static string getStaff(string staffId, string key)
  20. {
  21. Guid id;
  22. if (string.IsNullOrEmpty(staffId))
  23. {
  24. HttpRequest request = MyHttpContext.Current.Request;
  25. StringValues oo;
  26. request.Headers.TryGetValue("Sso-Token", out oo);
  27. if (oo.Count > 0 && oo.ToArray()[0] != "")
  28. {
  29. staffId = oo.ToArray()[0];
  30. }
  31. }
  32. //判断参数是否合法
  33. if (!string.IsNullOrEmpty(staffId) && Guid.TryParse(staffId, out id))
  34. {
  35. Object signtoken;
  36. MyHttpContext.Cache.TryGetValue(id.ToString(), out signtoken);
  37. var payload = new Dictionary<string, object>()
  38. {
  39. //{ "userid", userid },
  40. //{ "roleid", roleid },
  41. //{ "permission", permission }
  42. };
  43. var secretKey = TokenConfig.SecretKey;
  44. if (signtoken != null)
  45. {
  46. payload = Jwt.JsonWebToken.DecodeToObject((string)signtoken, secretKey);
  47. }
  48. else
  49. {
  50. return null;
  51. }
  52. Object userid;
  53. payload.TryGetValue(key, out userid);
  54. return userid == null ? null : userid.ToString();
  55. }
  56. else
  57. {
  58. return null;
  59. }
  60. }
  61. public static string setStaff(string key,string value, DateTime? Expire = null,string staffId = null)
  62. {
  63. Guid id;
  64. if (string.IsNullOrEmpty(staffId))
  65. {
  66. HttpRequest request = MyHttpContext.Current.Request;
  67. StringValues oo;
  68. request.Headers.TryGetValue("Sso-Token", out oo);
  69. if (oo.Count > 0 && oo.ToArray()[0] != "")
  70. {
  71. staffId = oo.ToArray()[0];
  72. }
  73. }
  74. //判断参数是否合法
  75. if (!string.IsNullOrEmpty(staffId) && Guid.TryParse(staffId, out id))
  76. {
  77. Object signtoken;
  78. MyHttpContext.Cache.TryGetValue(id.ToString(), out signtoken);
  79. var payload = new Dictionary<string, object>()
  80. {
  81. //{ "userid", userid },
  82. //{ "roleid", roleid },
  83. //{ "permission", permission }
  84. };
  85. var secretKey = TokenConfig.SecretKey;
  86. if (signtoken != null)
  87. {
  88. payload = Jwt.JsonWebToken.DecodeToObject((string)signtoken, secretKey);
  89. }
  90. else
  91. {
  92. return null;
  93. }
  94. Object userid;
  95. payload.Remove(key, out userid);
  96. payload.Add(key,value);
  97. string SignToken = Jwt.JsonWebToken.Encode(payload, secretKey, Jwt.JwtHashAlgorithm.HS256);
  98. var ExpireTime = Expire != null? Expire.Value: DateTime.Now.AddSeconds(TokenConfig.ExpireTime);
  99. ((MemoryCache)MyHttpContext.Cache).GetOrCreate(staffId, entry =>
  100. {
  101. entry.SetAbsoluteExpiration(ExpireTime);
  102. return SignToken;
  103. });
  104. return userid == null ? null : userid.ToString();
  105. }
  106. else
  107. {
  108. return null;
  109. }
  110. }
  111. public static void RemoveStaff(string key, string staffId = null)
  112. {
  113. Guid id;
  114. if (string.IsNullOrEmpty(staffId))
  115. {
  116. HttpRequest request = MyHttpContext.Current.Request;
  117. StringValues oo;
  118. request.Headers.TryGetValue("Sso-Token", out oo);
  119. if (oo.Count > 0 && oo.ToArray()[0] != "")
  120. {
  121. staffId = oo.ToArray()[0];
  122. }
  123. }
  124. //判断参数是否合法
  125. if (!string.IsNullOrEmpty(staffId) && Guid.TryParse(staffId, out id))
  126. {
  127. MyHttpContext.Cache.Remove(staffId);
  128. }
  129. }
  130. public static string getStaff(string key)
  131. {
  132. StringValues oo = MyHttpContext.Current.Request.Headers["Sso-Token"];
  133. string staffid = "";
  134. if (oo.Count > 0 && oo.ToArray()[0] != "")
  135. {
  136. staffid = oo.ToArray()[0];
  137. }
  138. if (staffid != "")
  139. return getStaff(staffid, key);
  140. else
  141. return null;
  142. }
  143. #region 获取Token
  144. /// <summary>
  145. /// 获得Cookie的值
  146. /// </summary>
  147. /// <param name="tokenName"></param>
  148. /// <returns></returns>
  149. public static string GetTokenValue(string tokenName)
  150. {
  151. return getStaff(tokenName);
  152. }
  153. /// <summary>
  154. /// 获得Cookie的值
  155. /// </summary>
  156. /// <param name="cookie"></param>
  157. /// <returns></returns>
  158. //public static string GetCookieValue(HttpCookie cookie)
  159. //{
  160. // if (cookie != null)
  161. // {
  162. // return cookie.Value;
  163. // }
  164. // return "";
  165. //}
  166. /// <summary>
  167. /// 获得Cookie
  168. /// </summary>
  169. /// <param name="tokenName"></param>
  170. /// <returns></returns>
  171. public static string GetToken(string tokenName)
  172. {
  173. return getStaff(tokenName);
  174. }
  175. #endregion
  176. #region 删除Cookie
  177. /// <summary>
  178. /// 删除Cookie
  179. /// </summary>
  180. /// <param name="tokenName"></param>
  181. public static void RemoveToken(string tokenName)
  182. {
  183. RemoveStaff(tokenName);
  184. }
  185. #endregion
  186. #region 设置/修改Cookie
  187. /// <summary>
  188. /// 设置Cookie
  189. /// </summary>
  190. /// <param name="tokenName"></param>
  191. /// <param name="key"></param>
  192. /// <param name="value"></param>
  193. /// <param name="expires"></param>
  194. public static void SetToken(string tokenName, string value, DateTime? expires)
  195. {
  196. //Guard.IsNotNullOrEmpty(tokenName, "tokenName");
  197. setStaff(tokenName, value);
  198. }
  199. #endregion
  200. #region 添加Cookie
  201. /// <summary>
  202. /// 添加为Cookie.Values集合
  203. /// </summary>
  204. /// <param name="tokenName"></param>
  205. /// <param name="key"></param>
  206. /// <param name="value"></param>
  207. /// <param name="expires"></param>
  208. public static void AddToken(string tokenName, string value, DateTime expires)
  209. {
  210. //Guard.IsNotNullOrEmpty(tokenName, "tokenName");
  211. //HttpCookie cookie = new HttpCookie(tokenName);
  212. //cookie.Expires = expires;
  213. //cookie.Value = value;
  214. //AddCookie(cookie);
  215. setStaff(tokenName, value, expires);
  216. }
  217. /// <summary>
  218. /// 添加Cookie
  219. /// </summary>
  220. /// <param name="cookie"></param>
  221. //public static void AddCookie(HttpCookie cookie)
  222. //{
  223. // HttpResponse response = MyHttpContext.Current.Response;
  224. // if (response != null)
  225. // {
  226. // //指定客户端脚本是否可以访问[默认为false]
  227. // cookie.HttpOnly = true;
  228. // //指定统一的Path,比便能通存通取
  229. // cookie.Path = "/";
  230. // //设置跨域,这样在其它二级域名下就都可以访问到了
  231. // //cookie.Domain = "nas.com";
  232. // response.AppendCookie(cookie);
  233. // }
  234. //}
  235. #endregion
  236. }
  237. }