1、创建Cookies有两种方法:

Response.Cookies["userName"].Value = "patrick";
Response.Cookies["userName"].Expires = DateTime.Now.AddDays(1);
HttpCookie aCookie = new HttpCookie("lastVisit");
aCookie.Value = DateTime.Now.ToString();
aCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(aCookie);

2、读取Cookie

if(Request.Cookies["userName"] != null)
Label1.Text = Server.HtmlEncode(Request.Cookies["userName"].Value); if(Request.Cookies["userName"] != null)
{
HttpCookie aCookie = Request.Cookies["userName"];
Label1.Text = Server.HtmlEncode(aCookie.Value);
}

3、修改和删除Cookie

int counter;
if (Request.Cookies["counter"] == null)
counter = 0;
else
{
counter = int.Parse(Request.Cookies["counter"].Value);
}
counter++; Response.Cookies["counter"].Value = counter.ToString();
Response.Cookies["counter"].Expires = DateTime.Now.AddDays(1);

一定要注意设置Cookies是用Response读取是用Request两者不一样!

删除Cookie

string subkeyName;
subkeyName = "userName";
HttpCookie aCookie = Request.Cookies["userInfo"];
aCookie.Values.Remove(subkeyName);
aCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(aCookie);
HttpCookie aCookie;
string cookieName;
int limit = Request.Cookies.Count;
for (int i=0; i<limit; i++)
{
cookieName = Request.Cookies[i].Name;
aCookie = new HttpCookie(cookieName);
aCookie.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(aCookie);
}

MVC Cookie的使用的更多相关文章

  1. Spring MVC Cookie example

    In this post we will see how to access and modify http cookies of a webpage in Spring MVC framework. ...

  2. spring mvc +cookie+拦截器功能 实现系统自动登陆

    先看看我遇到的问题: @ResponseBody @RequestMapping("/logout") public Json logout(HttpSession session ...

  3. ASP.NET MVC Cookie 身份验证

    1 创建一个ASP.NET MVC 项目 添加一个 AccountController 类. public class AccountController : Controller { [HttpGe ...

  4. mvc cookie

    Response.Cookies["userName"].Value = "ddd"; <%     if (Request.Cookies[" ...

  5. asp.net mvc cookie超时返回登录页面问题

    filterContext.HttpContext.Response.Write("<script>top.location.href = '/Login/Index';< ...

  6. [置顶]【实用 .NET Core开发系列】- 导航篇

    前言 此系列从出发点来看,是 上个系列的续篇, 上个系列因为后面工作的原因,后面几篇没有写完,后来.NET Core出来之后,注意力就转移到了.NET Core上,所以再也就没有继续下去,此是原因之一 ...

  7. 最近面试前端岗位,汇总了一下前端面试题(JS+CSS)

    JavaScript 运行机制 1. 单线程(用途决定,需要与用户互动以及操作DOM) 2. 分同步任务(主线程)与异步任务(任务队列),只有任务队列通知主线程某个任务可以执行了,该 任务才会进入主线 ...

  8. 大学四年,总结一套适合小白的Java自学路线和方法

    前言篇 大家好,我是bigsai 好久不见,甚是想念,文章同时收录在回车课堂(文底阅读原文可达). 无论你是大学生还是在职人员,想学Java时,都会面临两个选择,自学或者报班.报班通常太费钱,时间又不 ...

  9. JWT漏洞学习

    JWT漏洞学习 什么是JWT? JWT是JSON Web Token的缩写,它是一串带有声明信息的字符串,由服务端使用加密算法对信息签名,以保证其完整性和不可伪造性.Token里可以包含所有必要的信息 ...

随机推荐

  1. 虚幻4外包团队-推荐非常全面的的Unreal教程

    <Unreal SDK 游戏开发从入门到精通(UnrealScript语法.UI Scene界面.UDK独立开发游戏)> 课程讲师:Shark  课程分类:.net 适合人群:初级 课时数 ...

  2. 【mysql】Infobright和mysql数据入库性能测试

    产生测试文件 测试文件部分内容如下: 产生测试文件代码: package foo; import java.io.File; import java.io.FileWriter; import jav ...

  3. ruby中excel简单操作以及文件读取操作方法

    # -*-coding:utf-8 -*-#author:kanlijunrequire 'win32ole'require 'fileutils'class ResultAnalyse @@i=0 ...

  4. x64 PL/SQL 连接 Oralce 提示 Could not initialize oci.dll

    在 x64 的 Win10 上重新安装了 Oralce 后,通过 PL/SQL 连接数据库时,提示如下错误信息 环境 windows7 64bit Oracle win64 11gR2 PL/SQL ...

  5. Convert Excel data to MDB file

    所需组件: microsoft ado ext. 2.8 for ddl and security 或者更新的组件. 添加: using ADOX;using System.Runtime.Inter ...

  6. cocos2d-x3.2创建新项目失败的一种可能性(cygwin自带的python2.6被抢先执行)

    之前一直使用cocos2d-x2.2写游戏,写了几个游戏后,想尝试下3.x版本的新功能,就下载了cocos2d-x3.2版本. 参照官方文档的说法,cocos2d-x3.x版本需要python2.7环 ...

  7. Replication

    Peer-to-peer replication: http://msdn.microsoft.com/en-us/library/ms151196(v=sql.120).aspx Merge rep ...

  8. Metro Win8风格的按钮(Filp翻转)

    原地址->http://www.cnblogs.com/yk250/p/5661093.html 介绍:简约而不简单....颜色可随意调制,最好用Blend工具. 效果图如下:话说这个图会不会太 ...

  9. html狂记

    由于承接一部分站点优化工作,竟无节操地好几天没有喂博客,好了,今天完成交接工作,马上奉上DIV+CSS传统开发的干货一枚,内容绝非原创,仅是收集.学习.消化.总结.吐出... 基本结构标签: < ...

  10. hashmap两种遍历方法

    第一种:使用entryset来进行遍历 Map map=new HashMap(); Iterator iter=map.entrySet().iterator(); while(iter.hasNe ...