zoukankan      html  css  js  c++  java
  • 【.NET】Cookie操作类

     public static class CookiesHelper
        {
            /// <summary>
            /// Cookies赋值
            /// </summary>
            /// <param name="strName">主键</param>
            /// <param name="strValue">键值</param>
            /// <param name="strDay">有效天数</param>
            /// <returns></returns>
            public static bool SetCookie(string strName, string strValue, int strDay)
            {
                try
                {
                    HttpCookie Cookie = System.Web.HttpContext.Current.Request.Cookies[strName];
                    if (Cookie == null)
                    {
                        Cookie = new HttpCookie(strName);
                    }
                    Cookie.Expires = DateTime.Now.AddDays(strDay);
                    Cookie.Value = strValue;
                    System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
                    return true;
                }
                catch
                {
                    return false;
                }
            }
    
            /// <summary>
            /// 读取Cookies
            /// </summary>
            /// <param name="strName">主键</param>
            /// <returns></returns>
    
            public static string GetCookie(string strName)
            {
                HttpCookie Cookie = System.Web.HttpContext.Current.Request.Cookies[strName];
                if (Cookie != null)
                {
                    return Cookie.Value.ToString();
                }
                else
                {
                    return null;
                }
            }
    
            /// <summary>
            /// 删除Cookies
            /// </summary>
            /// <param name="strName">主键</param>
            /// <returns></returns>
            public static bool DelCookie(string strName)
            {
                try
                {
                    HttpCookie Cookie = System.Web.HttpContext.Current.Request.Cookies[strName];// new HttpCookie(strName);
                    Cookie.Expires = DateTime.Now.AddDays(-1);
                    System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }
  • 相关阅读:
    TCP /IP协议的理解
    Linux 文件大小跟踪命令
    关于3D旋转的本质
    Unity旋转
    QT解析XML(机械拆装)
    红黑树总结
    EXCEPTIONS
    算数运算符注意事项
    java中常用的转义字符
    常量和数据类型
  • 原文地址:https://www.cnblogs.com/zyj649261718/p/5000830.html
Copyright © 2011-2022 走看看