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
{
///
/// 不支持 cookie values
///
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()
{
//{ "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()
{
//{ "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
///
/// 获得Cookie的值
///
///
///
public static string GetTokenValue(string tokenName)
{
return getStaff(tokenName);
}
///
/// 获得Cookie的值
///
///
///
//public static string GetCookieValue(HttpCookie cookie)
//{
// if (cookie != null)
// {
// return cookie.Value;
// }
// return "";
//}
///
/// 获得Cookie
///
///
///
public static string GetToken(string tokenName)
{
return getStaff(tokenName);
}
#endregion
#region 删除Cookie
///
/// 删除Cookie
///
///
public static void RemoveToken(string tokenName)
{
RemoveStaff(tokenName);
}
#endregion
#region 设置/修改Cookie
///
/// 设置Cookie
///
///
///
///
///
public static void SetToken(string tokenName, string value, DateTime? expires)
{
//Guard.IsNotNullOrEmpty(tokenName, "tokenName");
setStaff(tokenName, value);
}
#endregion
#region 添加Cookie
///
/// 添加为Cookie.Values集合
///
///
///
///
///
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);
}
///
/// 添加Cookie
///
///
//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
}
}