一、无子键或单级cookie 读写
(1)、写入:
第一种
HttpCookie cookie=new HttpCookie("User");
cookie.Value="admin";
cookie.Expires=DateTime.Now.AddMinutes(1000);
HttpContext.Current.Response.AppendCookie(cookie);
或者
HttpContext.Current.Response.Cookies.Add(cookie);

第二种:
HttpContext.Current.Response.Cookies['User'].Value="admin";
HtttpContext.Current.Response.Cookies["User"].Exipres=DateTime.Now.AddMinutes(100);

(2)、读取
if(HttpContext.Current.Request.Cookies[key]!=null)
{
  string value=HttpContext.Current.Request.Cookies[key];
}
else
{
  string value="不存在"+key;
}
(3)、修改
if(HttpContext.Current.Request.Cookies[key]!=null)
{
  HttpCookie cookie=HttpCookie.Current.Request.Cookies[key];
  cookie.Value=value;
  HttpContext.Current.Response.Cookies.Add(cookie);
}
(4)、删除
if(HttpContext.Current.Request.Cookies[key]!=null)
{
  HttpCookie cookie=HttpContext.Current.Request.Cookies[key];
  cookie.Expires=DateTime.Now.AddMiuntes(time);//负数
  HttpContext.Current.Response.Cookies.Add(cookie);
}

二、有子键或多级cookie 读写

(1)、创建
HttpCookie cookie=new HttpCookie("user","admin");
或者
HttpCookie cookie=new HttpCookie("user");
cookie.Value="admin";
-------------------------------
cookie.Expires=DateTime.Now.AddMinutes(2);
cookie.Values["Name"]="Li";

cookie.Values.Add("Phone","12300000");
---------------------------------
HttpContext.Current.Response.Cookies.Add(cookie);

(2)、读取
if(HttpContext.Current.Request.Cookies[key]!=null)
{
  string value=HttpContext.Current.Request.Cookies[key][subkey] ?? "不存在:"+key+"->"+subkey;
}
else
{
  string value="不存在"+key;
}

(3)、修改
if(HttpContext.Current.Request.Cookies[key]!=null)
{
  HttpCookie cookie=HttpCookie.Current.Request.Cookies[key];
  cookie[subkey].Value=value;
  HttpContext.Current.Response.Cookies.Add(cookie);
}

asp.net 关于cookie的操作的更多相关文章

  1. asp.net对cookie的操作

    创建cookie: HttpCookie cookie = new HttpCookie("CurrentUser"); //创建一个名称为CurrentUser 的cookie对 ...

  2. .NET跨平台之旅:ASP.NET Core从传统ASP.NET的Cookie中读取用户登录信息

    在解决了asp.net core中访问memcached缓存的问题后,我们开始大踏步地向.net core进军——将更多站点向asp.net core迁移,在迁移涉及获取用户登录信息的站点时,我们遇到 ...

  3. ASP.NET之Cookie(坑爹的Response.Cookies.Remove)(转)

      在web开发中Cookie是必不可少的 .NET自然也有一个强大的Cookie操作类,我们用起来也非常方便,不过在使用中我们会发现一个坑爹的事情Response.Cookies.Remove删除不 ...

  4. ASP.NET的Cookie和Session

    HTTP属于应用层,HTTP协议一共有五大特点:1.支持客户/服务器模式;2.简单快速;3.灵活;4.无连接;5.无状态. 无状态HTTP协议是无状态的协议.一旦数据交换完毕,客户端与服务器端的连接就 ...

  5. ASP.NET之Cookie(坑爹的Response.Cookies.Remove)

    原文:ASP.NET之Cookie(坑爹的Response.Cookies.Remove) 在web开发中Cookie是必不可少的 .NET自然也有一个强大的Cookie操作类,我们用起来也非常方便, ...

  6. ASP.NET 初识Cookie

    1.ASP.NET中使用Cookie 0.说明 Cookie存在客户端电脑上,Session存在服务器上,所以保存登录信息等敏感信息时不能使用Cookie,用户个性化设置可以使用Cookie 1.新建 ...

  7. ASP.NET -- WebForm -- Cookie的使用 应用程序权限设计 权限设计文章汇总 asp.net后台管理系统-登陆模块-是否自动登陆 C# 读写文件摘要

    ASP.NET -- WebForm -- Cookie的使用 ASP.NET -- WebForm --  Cookie的使用 Cookie是存在浏览器内存或磁盘上. 1. Test3.aspx文件 ...

  8. ASP.NET中Cookie跨域的问题及解决代码

    ASP.NET中Cookie跨域的问题及解决代码 http://www.liyumei.net.cn/post/share18.html Cookies揭秘  http://www.cnblogs.c ...

  9. ASP.NET中cookie与Fiter实现简单登陆,AllowAnonymous匿名登陆

    向服务器发送cookie 在登陆的时候,我们可以可以通过下列代码,向服务器发送cookie,其中包括自己的账号信息(不涉及加密),用以后面判断访问者. HttpCookie cookie = new ...

随机推荐

  1. 嵌入式(C)笔试题

    1 读程序段,回答问题 (a) int main(int argc,char *argv[]) { int c=9,d=0; c=c++%5; d=c; printf("d=%d\n&quo ...

  2. java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized

    Exception in thread "main" java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä ...

  3. Node.js使用cnpm

    npm是Node.js中维护第三方库.模块的工具,可是国外的速度非常悲剧,这里有一个中国的源cnpm. http://cnpmjs.org/ 须要在命令行中执行 npm install -g cnpm ...

  4. 程序设计基石与实践系列之编写高效的C程序与C代码优化

    原文出处: codeproject:Writing Efficient C and C Code Optimization 虽然对于优化C代码有非常多有效的指导方针,可是对于彻底地了解编译器和你工作的 ...

  5. EJB3.0高速入门项目开发步骤

    EJB3.0开发步骤 1.   开发环境 IDE开发工具:Eclipse Java EE IDE for Web Developers EJB容器:jboss-4.2.3.GA 后台数据库:MysQL ...

  6. android 自己定义dialog并实现失去焦点(背景透明)的功能

    前言:因为在项目中须要用到更新显示动画的需求,所以想到了dialog,自己定义dialog不难.网上教程非常多,可是在实现dialog背景透明的需求时,遇到了一点问题.网上的一些方法在我的机器上并没有 ...

  7. RvmTranslator for Linux

    RvmTranslator for Linuxeryar@163.com RvmTranslator can translate the RVM file exported by AVEVA Plan ...

  8. 一站式学习WireShark

    一 基础 http://blog.sina.com.cn/s/blog_987e00020102x5k1.html 选中一个数据包,右键选中某一个数据包,然后follow-->Tcp Strea ...

  9. Find or Query Data with C# Driver

    https://docs.mongodb.com/getting-started/csharp/query/ Overview You can use the Find and FindAsync m ...

  10. metasploit.meterpreter学习笔记(博主推荐)

    Metasploit学习笔记(博主推荐) 继续上面的博客 metasploit.meterpreter的基本使用: 首先来获取当前系统(即xp)下的正在运行的一些进程 获得进程之后,我们通过migra ...