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

2021-04-12 16:29

阅读:500

标签:ddc   response   cookie   string   添加   tpc   lse   arc   void   


public class CookieHelper
{
///


/// 1.1添加Cookie
///

///
///
public static void AddCookie(string ckname, string ckvalue)
{
AddCookie(ckname, ckvalue, DateTime.Now.AddDays(1.0));
}
///
/// 1.2添加一个Cookie
///

/// cookie名
/// cookie值
/// 过期时间 DateTime
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);
}
///
/// 2读取cookie
///

/// ckname
///
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;
}
///
/// 3修改cookie
///

///
///
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);
}
}
///
/// 4删除cookie
///

/// ckname
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);
}
}


}

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

标签:ddc   response   cookie   string   添加   tpc   lse   arc   void   

原文地址:https://www.cnblogs.com/zoood/p/12396802.html


评论


亲,登录后才可以留言!