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;
                }
            }
        }
  • 相关阅读:
    numpy数组中round, around, rint, ceil, floor, modf, trunc, fix
    基于KNN算法实现手写数字识别
    PM2.5预测(李宏毅机器学习hw_1)
    numpy的array和asarray
    iOS socket
    UIScrollView
    ios读取文件
    CGContext绘图
    UINavigationController和UITabBarController合用
    window下svn开机自动启动
  • 原文地址:https://www.cnblogs.com/zyj649261718/p/5000830.html
Copyright © 2011-2022 走看看