zoukankan      html  css  js  c++  java
  • C# CookieHelper

    using System;
    using System.Text.RegularExpressions;
    using System.Web;
    
    public class CookieHelper
    {
        /// <summary>
        /// 清除登录用户的cookie
        /// </summary>
        public static void ClearUserCookie(string cookieDomain, string cookiesName)
        {
            HttpCookie cookie = new HttpCookie(cookiesName);
            cookie.Values.Clear();
            cookie.Expires = DateTime.Now.AddYears(-1);
            //string cookieDomain = ConfigFactory.GetConfig().CookieDomain.Trim();
            if (cookieDomain != string.Empty &&
                HttpContext.Current.Request.Url.Host.IndexOf(cookieDomain, System.StringComparison.Ordinal) > -1
                && IsValidDomain(HttpContext.Current.Request.Url.Host))
                cookie.Domain = cookieDomain;
            HttpContext.Current.Response.AppendCookie(cookie);
        }
    
        /// <summary>
        /// 读cookie值
        /// </summary>
        /// <param name="strName">名称</param>
        /// <returns>cookie值</returns>
        public static string GetCookie(string strName)
        {
            var httpCookie = HttpContext.Current.Request.Cookies[strName];
            return httpCookie != null ? httpCookie.Value : "";
        }
    
        /// <summary>
        /// 获得cookie值
        /// </summary>
        /// <param name="cookiesName">Cookie 名称</param>
        /// <param name="strName"></param>
        /// <returns></returns>
        public static string GetCookie(string cookiesName, string strName)
        {
            var httpCookie = HttpContext.Current.Request.Cookies[cookiesName];
            if (httpCookie != null && httpCookie[strName] != null)
            {
                return HttpUtility.UrlDecode(httpCookie.Values[strName]);
            }
    
            return "";
        }
    
        /// <summary>
        /// 是否为有效域
        /// </summary>
        /// <param name="host">域名</param>
        /// <returns></returns>
        public static bool IsValidDomain(string host)
        {
            Regex r = new Regex(@"^d+$");
            if (host.IndexOf(".") == -1)
            {
                return false;
            }
            return !r.IsMatch(host.Replace(".", string.Empty));
        }
    
        /// <summary>
        /// 写cookie值,不在客户端创建文件,存放在服务器内存中。
        /// </summary>
        /// <param name="strName">名称</param>
        /// <param name="strValue"></param>
        public static void WriteCookie(string strName, string strValue)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
            if (cookie == null)
            {
                cookie = new HttpCookie(strName);
            }
            cookie.Value = strValue;
            HttpContext.Current.Response.AppendCookie(cookie);
        }
    
        /// <summary>
        /// 写cookie值,在客户端创建文件。存放cookie值(单一项值)。
        /// </summary>
        /// <param name="strName">名称</param>
        /// <param name="strValue"></param>
        /// <param name="cookieDomain"></param>
        /// <param name="expires">cookie 保存时长 单位分种</param>
        public static void WriteCookie(string strName, string strValue, string cookieDomain, int expires)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[strName] ?? new HttpCookie(strName);
            cookie.Value = strValue;
            //cookie.Expires = DateTime.Now.AddMinutes(expires);
            cookie.Expires = DateTime.Now.AddMinutes(expires);
            cookie.Domain = cookieDomain;
            HttpContext.Current.Response.AppendCookie(cookie);
        }
    
        /// <summary>
        /// 写cookie值(存放数组形式参数,用于第一次写cookie值,并指定有效时间)
        /// </summary>
        /// <param name="cookieName">cookies名字</param>
        /// <param name="strValuesName">cookie项的名称,cookie[strValuesName][]</param>
        /// <param name="strValue">cookie项的值,cookie[strValuesName][strValue]</param>
        /// <param name="cookieDomain">cookie域属性</param>
        /// <param name="expiresDays">cookies 有效时间 单位天</param>
        public static void WriteCookie(string cookieName, string strValuesName, string strValue, string cookieDomain, int expiresDays)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
            if (cookie == null)
            {
                cookie = new HttpCookie(cookieName);
                cookie.Values[strValuesName] = HttpUtility.UrlEncode(strValue);
            }
    
            cookie.Values[strValuesName] = HttpUtility.UrlEncode(strValue);
            cookie.Values["expires"] = expiresDays.ToString();
            cookie.Expires = DateTime.Now.AddDays(expiresDays);
    
            if (cookieDomain != string.Empty
                && HttpContext.Current.Request.Url.Host.IndexOf(cookieDomain) > -1
                && IsValidDomain(HttpContext.Current.Request.Url.Host))
                cookie.Domain = cookieDomain;
    
            HttpContext.Current.Response.AppendCookie(cookie);
        }
    
        /// <summary>
        /// 写cookie值(操作已经存在的cookie,存放数组形式参数)
        /// </summary>
        /// <param name="cookieName">cookies名字</param>
        /// <param name="strValuesName">cookie项的名称,cookie[strValuesName][]</param>
        /// <param name="strValue">cookie项的值,cookie[strValuesName][strValue]</param>
        /// <param name="cookieDomain">cookie域属性</param>
        public static void WriteCookie(string cookieName, string strValuesName, string strValue, string cookieDomain)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
            if (cookie == null)
            {
                cookie = new HttpCookie(cookieName);
                cookie.Values[strValuesName] = HttpUtility.UrlEncode(strValue);
            }
            else
            {
                cookie.Values[strValuesName] = HttpUtility.UrlEncode(strValue);
    
                var httpCookie = HttpContext.Current.Request.Cookies[cookieName];
                if (httpCookie != null && httpCookie["expires"] != null)
                {
                    int intExpires = 0;
                    if (httpCookie["expires"] != null)
                    {
                        try
                        {
                            intExpires = Convert.ToInt32(httpCookie["expires"]);
                        }
                        catch { }
                    }
    
                    if (intExpires > 0)
                    {
                        cookie.Values["expires"] = intExpires.ToString();
                        cookie.Expires = DateTime.Now.AddMinutes(intExpires);
                    }
                }
            }
    
            if (cookieDomain != string.Empty &&
                HttpContext.Current.Request.Url.Host.IndexOf(cookieDomain, System.StringComparison.Ordinal) > -1 &&
                IsValidDomain(HttpContext.Current.Request.Url.Host))
                cookie.Domain = cookieDomain;
    
            HttpContext.Current.Response.AppendCookie(cookie);
        }
    }
  • 相关阅读:
    Java 运用流传输文件
    Java-Web 用html和css写一个EasyMall注册界面
    Java-Web 用html写一个简单的用户注册界面
    Java-Web DOM方式解析xml文件
    弹窗的淡入淡出
    html5 indexDB的使用
    nodejs生成UID(唯一标识符)——node-uuid模块
    CSS3 Flex布局(伸缩布局盒模型)学习
    map area 标签的使用
    CSS3 伸缩布局盒模型
  • 原文地址:https://www.cnblogs.com/ilookbo/p/4988472.html
Copyright © 2011-2022 走看看