• MVC中Cookie的用法(二)---CookieHelper



    public class CookieHelper
    {
    /// <summary>
    /// 1.1添加Cookie
    /// </summary>
    /// <param name="ckname"></param>
    /// <param name="ckvalue"></param>
    public static void AddCookie(string ckname, string ckvalue)
    {
    AddCookie(ckname, ckvalue, DateTime.Now.AddDays(1.0));
    }
    /// <summary>
    /// 1.2添加一个Cookie
    /// </summary>
    /// <param name="ckname">cookie名</param>
    /// <param name="ckvalue">cookie值</param>
    /// <param name="expires">过期时间 DateTime</param>
    public static void AddCookie(string ckname, string ckvalue, DateTime expires)
    {
    HttpCookie cookie = new HttpCookie(ckname)
    {
    Value = ckvalue,
    Expires = expires
    };
    HttpContext.Current.Response.Cookies.Add(cookie);
    }
    /// <summary>
    /// 2读取cookie
    /// </summary>
    /// <param name="ckname">ckname</param>
    /// <returns></returns>
    public static string ReadCookie(string ckname)
    {
    HttpCookie cookie = HttpContext.Current.Request.Cookies[ckname];
    string str = string.Empty;
    if (cookie != null)
    {
    str = cookie.Value;
    }
    return str;
    }
    /// <summary>
    /// 3修改cookie
    /// </summary>
    /// <param name="ckname"></param>
    /// <param name="ckvalue"></param>
    public static void EditCookie(string ckname, string ckvalue)
    {
    if (HttpContext.Current.Request.Cookies[ckname] == null)
    {
    AddCookie(ckname, ckvalue, DateTime.Now.AddDays(1.0));
    }
    else
    {
    HttpContext.Current.Response.Cookies[ckname].Value = ckvalue;
    HttpContext.Current.Response.Cookies[ckname].Expires = DateTime.Now.AddDays(1);
    }
    }
    /// <summary>
    /// 4删除cookie
    /// </summary>
    /// <param name="ckname">ckname</param>
    public static void ClearCookie(string ckname)
    {
    HttpCookie cookie = HttpContext.Current.Request.Cookies[ckname];
    if (cookie != null)
    {
    cookie.Expires = DateTime.Now.AddYears(-3);
    HttpContext.Current.Response.Cookies.Add(cookie);
    }
    }


    }

  • 相关阅读:
    APK自我保护方法
    Andorid APK反逆向解决方案---梆梆加固原理探寻
    判断android文件是否加壳
    java调用dll-JNA
    Java调用本地接口
    pat00-自测2. 素数对猜想 (20)
    pat00-自测4. Have Fun with Numbers (20)
    pat00-自测3. 数组元素循环右移问题 (20)
    pat00-自测1. 打印沙漏(20)
    pat1013. Battle Over Cities (25)
  • 原文地址:https://www.cnblogs.com/zoood/p/12396802.html
Copyright © 2020-2023  润新知