public CookieHelper(IHttpContextAccessor httpContext) { _httpAccessor = httpContext; s_Mobile = new MobileCookiePersister(this._httpAccessor); s_Web = new WebCookiePersister(_httpAccessor); s_Normal = new NormalCookie(); s_Security = new SecurityCookie(); s_HighSecurity = new HighSecurityCookie(httpContext); }
/// <summary> /// 读取Cookie /// </summary> /// <typeparam name="T">返回的类型</typeparam> /// <param name="nodeName">配置的Cookie节点名,若未配置,则使用此名作为cookie存储名且使用默认配置</param> /// <returns></returns> public static T GetCookie <T>(string nodeName) { string persistType; string securityLevel; Dictionary <string, string> parameters; LoadConfig(nodeName, out persistType, out securityLevel, out parameters); ICookiePersist persister = CreatePersister(persistType); string cookieValue = persister.Get(GetCookieName(nodeName, parameters), parameters); ICookieEncryption safer = CreateCookieHelper(securityLevel); return(safer.DecryptCookie <T>(cookieValue, parameters)); }
/// <summary> /// 保存Cookie /// </summary> /// <typeparam name="T">需要存放的Cookie值的类型</typeparam> /// <param name="nodeName">配置的Cookie节点名,若未配置,则使用此名作为cookie存储名且使用默认配置</param> /// <param name="obj">需要存放的Cookie值</param> public static void SaveCookie <T>(string nodeName, T obj) { string persistType; string securityLevel; Dictionary <string, string> parameters; LoadConfig(nodeName, out persistType, out securityLevel, out parameters); ICookiePersist persister = CreatePersister(persistType); ICookieEncryption safer = CreateCookieHelper(securityLevel); string cookieValue = safer.EncryptCookie <T>(obj, parameters); persister.Save(GetCookieName(nodeName, parameters), cookieValue, parameters); }
public static void Set <T>(string nodeName, T obj, int?expiredMinutes = null) { string persistType; string securityLevel; Dictionary <string, string> parameters; LoadConfig(nodeName, out persistType, out securityLevel, out parameters); if (expiredMinutes.HasValue) { parameters["expires"] = expiredMinutes.Value.ToString(); } ICookiePersist persister = CreatePersister(persistType); ICookieEncryption safer = CreateCookieHelper(securityLevel); string cookieValue = safer.EncryptCookie <T>(obj, parameters); persister.Save(GetCookieName(nodeName, parameters), cookieValue, parameters); }