| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- using System;
- using System.Globalization;
- using static System.Web.Configuration.WebConfigurationManager;
- using System.Web;
- namespace CommonTool
- {
- /// <summary>
- /// Cookie帮助类
- /// </summary>
- public class CookieHelper
- {
- public static CookieHelper Instance { get; } = new CookieHelper();
- /// <summary>
- /// 写cookie值
- /// </summary>
- /// <param name="strName">名称</param>
- /// <param name="strValue">值</param>
- public void WriteCookie(string strName, string strValue)
- {
- HttpCookie cookie = HttpContext.Current.Request.Cookies[strName] ?? new HttpCookie(strName);
- cookie.Value = strValue;
- HttpContext.Current.Response.AppendCookie(cookie);
- }
- /// <summary>
- /// 写cookie值
- /// </summary>
- /// <param name="strName">名称</param>
- /// <param name="strValue">值</param>
- /// <param name="expires">过期时间(分钟)</param>
- public void WriteCookie(string strName, string strValue, int expires)
- {
- HttpCookie cookie = HttpContext.Current.Request.Cookies[strName] ?? new HttpCookie(strName);
- cookie.Value = strValue;
- cookie.Expires = DateTime.Now.AddMinutes(expires);
- HttpContext.Current.Response.AppendCookie(cookie);
- }
- /// <summary>
- /// 读cookie值
- /// </summary>
- /// <param name="strName">名称</param>
- /// <returns>cookie值</returns>
- public string GetCookie(string strName)
- {
- if (HttpContext.Current.Request.Cookies[strName] != null)
- {
- return HttpContext.Current.Request.Cookies[strName].Value;
- }
- return "";
- }
- /// <summary>
- /// Get cookie expiry date that was set in the cookie value
- /// </summary>
- /// <param name="cookie"></param>
- /// <returns></returns>
- public DateTime GetExpirationDate(HttpCookie cookie)
- {
- if (String.IsNullOrEmpty(cookie.Value))
- {
- return DateTime.MinValue;
- }
- string strDateTime = cookie.Value.Substring(cookie.Value.IndexOf("|", StringComparison.Ordinal) + 1);
- return Convert.ToDateTime(strDateTime);
- }
- /// <summary>
- /// Set cookie value using the token and the expiry date
- /// </summary>
- /// <param name="value"></param>
- /// <param name="minutes"></param>
- /// <returns></returns>
- public string BuildCookueValue(string value, int minutes)
- {
- return $"{value}|{DateTime.Now.AddMinutes(minutes).ToString(CultureInfo.InvariantCulture)}";
- }
- /// <summary>
- /// Reads cookie value from the cookie
- /// </summary>
- /// <param name="cookie"></param>
- /// <returns></returns>
- public string GetCookieValue(HttpCookie cookie)
- {
- if (cookie!=null)
- {
- if (String.IsNullOrEmpty(cookie.Value))
- return cookie.Value;
- return cookie.Value.Substring(0, cookie.Value.IndexOf("|", StringComparison.Ordinal));
- }
- return null;
- }
- public void DelCookie(string strName)
- {
- WriteCookie(strName, "", -10000);
- }
- public SysUserInfo GetSysUserInfo(string pcCookieName)
- {
- string lcCookieName = pcCookieName?? AppSettings["SysUserInfoCookie"];
- string lcCookieValue = GetCookieValue(HttpContext.Current.Request.Cookies[lcCookieName]);
- try
- {
- return JsonHelper.Instance.Deserialize<SysUserInfo>(SysSecurity.Decrypt(lcCookieValue));
- }
- catch
- {
- return null;
- }
- }
- }
- }
|