码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2020-03-02 17:37:28      阅读:83      评论:0      收藏:0      [点我收藏+]

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


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


}

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

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

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!