123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- using Common.Config;
- using Common.Http;
- using Microsoft.AspNetCore.Http;
- using Microsoft.Extensions.Caching.Memory;
- using Microsoft.Extensions.Primitives;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Web;
- //using Liger.Common;
- namespace LigerRM.Common
- {
- /// <summary>
- /// 不支持 cookie values
- /// </summary>
- public class SsoTokenHelper
- {
- public static string getStaff(string staffId, string key)
- {
- Guid id;
- if (string.IsNullOrEmpty(staffId))
- {
- HttpRequest request = MyHttpContext.Current.Request;
- StringValues oo;
- request.Headers.TryGetValue("Sso-Token", out oo);
- if (oo.Count > 0 && oo.ToArray()[0] != "")
- {
- staffId = oo.ToArray()[0];
- }
- }
- //判断参数是否合法
- if (!string.IsNullOrEmpty(staffId) && Guid.TryParse(staffId, out id))
- {
- Object signtoken;
- MyHttpContext.Cache.TryGetValue(id.ToString(), out signtoken);
- var payload = new Dictionary<string, object>()
- {
- //{ "userid", userid },
- //{ "roleid", roleid },
- //{ "permission", permission }
- };
- var secretKey = TokenConfig.SecretKey;
- if (signtoken != null)
- {
- payload = Jwt.JsonWebToken.DecodeToObject((string)signtoken, secretKey);
- }
- else
- {
- return null;
- }
- Object userid;
- payload.TryGetValue(key, out userid);
- return userid == null ? null : userid.ToString();
- }
- else
- {
- return null;
- }
- }
- public static string setStaff(string key,string value, DateTime? Expire = null,string staffId = null)
- {
- Guid id;
- if (string.IsNullOrEmpty(staffId))
- {
- HttpRequest request = MyHttpContext.Current.Request;
- StringValues oo;
- request.Headers.TryGetValue("Sso-Token", out oo);
- if (oo.Count > 0 && oo.ToArray()[0] != "")
- {
- staffId = oo.ToArray()[0];
- }
- }
- //判断参数是否合法
- if (!string.IsNullOrEmpty(staffId) && Guid.TryParse(staffId, out id))
- {
- Object signtoken;
- MyHttpContext.Cache.TryGetValue(id.ToString(), out signtoken);
- var payload = new Dictionary<string, object>()
- {
- //{ "userid", userid },
- //{ "roleid", roleid },
- //{ "permission", permission }
- };
- var secretKey = TokenConfig.SecretKey;
- if (signtoken != null)
- {
- payload = Jwt.JsonWebToken.DecodeToObject((string)signtoken, secretKey);
- }
- else
- {
- return null;
- }
- Object userid;
- payload.Remove(key, out userid);
- payload.Add(key,value);
-
- string SignToken = Jwt.JsonWebToken.Encode(payload, secretKey, Jwt.JwtHashAlgorithm.HS256);
-
- var ExpireTime = Expire != null? Expire.Value: DateTime.Now.AddSeconds(TokenConfig.ExpireTime);
- ((MemoryCache)MyHttpContext.Cache).GetOrCreate(staffId, entry =>
- {
- entry.SetAbsoluteExpiration(ExpireTime);
- return SignToken;
- });
- return userid == null ? null : userid.ToString();
- }
- else
- {
- return null;
- }
- }
- public static void RemoveStaff(string key, string staffId = null)
- {
- Guid id;
- if (string.IsNullOrEmpty(staffId))
- {
- HttpRequest request = MyHttpContext.Current.Request;
- StringValues oo;
- request.Headers.TryGetValue("Sso-Token", out oo);
- if (oo.Count > 0 && oo.ToArray()[0] != "")
- {
- staffId = oo.ToArray()[0];
- }
- }
- //判断参数是否合法
- if (!string.IsNullOrEmpty(staffId) && Guid.TryParse(staffId, out id))
- {
- MyHttpContext.Cache.Remove(staffId);
- }
- }
- public static string getStaff(string key)
- {
- StringValues oo = MyHttpContext.Current.Request.Headers["Sso-Token"];
- string staffid = "";
- if (oo.Count > 0 && oo.ToArray()[0] != "")
- {
- staffid = oo.ToArray()[0];
- }
- if (staffid != "")
- return getStaff(staffid, key);
- else
- return null;
- }
- #region 获取Token
- /// <summary>
- /// 获得Cookie的值
- /// </summary>
- /// <param name="tokenName"></param>
- /// <returns></returns>
- public static string GetTokenValue(string tokenName)
- {
- return getStaff(tokenName);
- }
- /// <summary>
- /// 获得Cookie的值
- /// </summary>
- /// <param name="cookie"></param>
- /// <returns></returns>
- //public static string GetCookieValue(HttpCookie cookie)
- //{
- // if (cookie != null)
- // {
- // return cookie.Value;
- // }
- // return "";
- //}
- /// <summary>
- /// 获得Cookie
- /// </summary>
- /// <param name="tokenName"></param>
- /// <returns></returns>
- public static string GetToken(string tokenName)
- {
- return getStaff(tokenName);
- }
- #endregion
- #region 删除Cookie
- /// <summary>
- /// 删除Cookie
- /// </summary>
- /// <param name="tokenName"></param>
- public static void RemoveToken(string tokenName)
- {
- RemoveStaff(tokenName);
- }
- #endregion
- #region 设置/修改Cookie
-
- /// <summary>
- /// 设置Cookie
- /// </summary>
- /// <param name="tokenName"></param>
- /// <param name="key"></param>
- /// <param name="value"></param>
- /// <param name="expires"></param>
- public static void SetToken(string tokenName, string value, DateTime? expires)
- {
- //Guard.IsNotNullOrEmpty(tokenName, "tokenName");
- setStaff(tokenName, value);
- }
- #endregion
- #region 添加Cookie
- /// <summary>
- /// 添加为Cookie.Values集合
- /// </summary>
- /// <param name="tokenName"></param>
- /// <param name="key"></param>
- /// <param name="value"></param>
- /// <param name="expires"></param>
- public static void AddToken(string tokenName, string value, DateTime expires)
- {
- //Guard.IsNotNullOrEmpty(tokenName, "tokenName");
- //HttpCookie cookie = new HttpCookie(tokenName);
- //cookie.Expires = expires;
- //cookie.Value = value;
- //AddCookie(cookie);
- setStaff(tokenName, value, expires);
- }
- /// <summary>
- /// 添加Cookie
- /// </summary>
- /// <param name="cookie"></param>
- //public static void AddCookie(HttpCookie cookie)
- //{
- // HttpResponse response = MyHttpContext.Current.Response;
- // if (response != null)
- // {
- // //指定客户端脚本是否可以访问[默认为false]
- // cookie.HttpOnly = true;
- // //指定统一的Path,比便能通存通取
- // cookie.Path = "/";
- // //设置跨域,这样在其它二级域名下就都可以访问到了
- // //cookie.Domain = "nas.com";
- // response.AppendCookie(cookie);
- // }
- //}
- #endregion
- }
-
- }
|