Cookie的读写

Posted htb

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Cookie的读写相关的知识,希望对你有一定的参考价值。

[HttpPost]
public ActionResult Login()
{
//写入cookies方式1
HttpCookie cookie = new HttpCookie("tempToken");
cookie.Value = "123456";
cookie.Expires = DateTime.Now.AddDays(7);
Response.Cookies.Add(cookie);

//写入cookies方式2
Response.Cookies["tempToken"].Value = "123456";
Response.Cookies["tempToken"].Expires = DateTime.Now.AddDays(7);
return Json(new { message="cookie设置成功"});
}

[HttpPost]
public ActionResult GainCookie()
{
//读取cookies
string cook="";
if (Request.Cookies["tempToken"]!=null)
{
cook = Request.Cookies["tempToken"].Value;
}
return Json(new { message=cook});
}

[HttpPost]
public ActionResult ClearCookie()
{
//清除cookies
Response.Cookies["tempToken"].Expires = DateTime.Now.AddDays(-1);
return Json(new { message="清除成功"});
}

以上是关于Cookie的读写的主要内容,如果未能解决你的问题,请参考以下文章

js读写Cookie问题(Cookie存储时长Cookie存储域)汇总

Cookie的读写

js对Cookie的读写操作

Cookie 读写详解

使用 Ruby on Rails 4 和 Nginx 读写 cookie

JS对Cookie的读写删除