123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- using NIU.Core;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Web;
- namespace CP.Common
- {
- /// <summary>
- /// Cookie类
- /// </summary>
- public class Cookies
- {
- /// <summary>
- /// cookie域
- /// </summary>
- private static string _Domain = ".niu.cn";
- /// <summary>
- /// cookie名称
- /// </summary>
- public static string _CookieAdminName = "_NIU_ADMIN";
- /// <summary>
- /// cookie名称(权限)
- /// </summary>
- public static string _AuthorityName = "_NIU_ADMIN_Authority";
- /// <summary>
- /// 获取cookie
- /// </summary>
- /// <param name="cookieName">cookie名称</param>
- /// <returns></returns>
- public static string Get(string cookieName, string domain = ".niu.cn")
- {
- if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[cookieName] != null)
- {
- //HttpContext.Current.Request.Cookies[cookieName].Domain = domain;
- string value = HttpContext.Current.Request.Cookies[cookieName].Value.ToString();
- return Encryption.Get(value, _CookieAdminName);
- }
- return "";
- }
- /// <summary>
- /// 写入cookie
- /// </summary>
- /// <param name="cookieName">cookie名称</param>
- /// <param name="cookieValue">cookie值</param>
- /// <param name="Expires">过期时间(分钟)</param>
- public static void Set(string cookieName, string cookieValue, int Expires = 0, string domain = ".niu.cn")
- {
- HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
- if (cookie == null)
- {
- cookie = new HttpCookie(cookieName);
- }
- cookie.Value = Encryption.Set(cookieValue, _CookieAdminName);
- //cookie.Domain = domain;
- cookie.HttpOnly = true;
- if (Expires > 0)
- cookie.Expires = DateTime.Now.AddMinutes(Expires);
- else
- cookie.Expires = DateTime.Now.AddMinutes(60);
- HttpContext.Current.Response.AppendCookie(cookie);
- }
- /// <summary>
- /// 清除COOKIES
- /// </summary>
- public static void Delete(string cookieName, string domain = ".niu.cn")
- {
- //HttpContext.Current.Response.Cookies[cookieName].Domain = domain;
- HttpContext.Current.Response.Cookies[cookieName].Expires = DateTime.Now.AddDays(-600);
- }
- /// <summary>
- /// 移除Cookies
- /// </summary>
- /// <param name="cookiesName"></param>
- public static void RemoveCookies(string cookiesName)
- {
- if (string.IsNullOrEmpty(cookiesName))
- return;
- HttpCookie cookie = HttpContext.Current.Request.Cookies[cookiesName];
- if (null != cookie)
- {
- //HttpContext.Current.Response.Cookies[cookiesName].Domain = _Domain;
- HttpContext.Current.Response.Cookies[cookiesName].Expires = DateTime.Now.AddDays(-600);
- HttpContext.Current.Response.Cookies[cookiesName].Values.Clear();
- cookie.Expires = DateTime.Now.AddDays(-5);
- HttpContext.Current.Response.Cookies.Add(cookie);
- }
- }
- /// <summary>
- /// 写Cookie值
- /// </summary>
- /// <param name="strName">键</param>
- /// <param name="strValue">名</param>
- /// <param name="httponly">httponly默认true</param>
- /// <param name="expires">默认480分钟</param>
- public static void WriteCookie(string strName, string strValue, int expires = 480, bool httponly = true, string domain = ".niu.cn")
- {
- HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
- if (cookie == null)
- {
- cookie = new HttpCookie(strName);
- }
- cookie.HttpOnly = httponly;
- //cookie.Domain = domain;
- cookie.Value = HttpContext.Current.Server.UrlEncode(strValue);
- if (expires > 0)
- cookie.Expires = DateTime.Now.AddMinutes(expires);
- HttpContext.Current.Response.Cookies.Add(cookie);
- }
- public static string GetCookieBy(string strName, string domain = ".niu.cn")
- {
- if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null)
- {
- //HttpContext.Current.Request.Cookies[strName].Domain = domain;
- return HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request.Cookies[strName].Value.ToString());
- }
- return "";
- }
- }
- }
|