一、无子键或单级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. 2015 Multi-University Training Contest 3 hdu 5316 Magician

    Magician Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  2. 国庆 day 7 下午

    思路:见博客. #include<iostream> #include<cstdio> #include<cstring> #include<algorith ...

  3. js 判断是否滚动到底部

    $(window).scroll(function(){ var scrollTop = $(this).scrollTop(); //scrollTop() 方法返回或设置匹配元素的滚动条的垂直位置 ...

  4. Building a Space Station POJ 2031 【最小生成树 prim】

    http://poj.org/problem?id=2031 Description You are a member of the space station engineering team, a ...

  5. 树莓派安装node.js

    因为树莓派是arm架构,node.js上并没有提供arm架构的二进制包下载. 试过使用apt包管理安装和直接node.js源代码编译安装(编译了四五个小时...),都没有成功,报非法指令错误,预计还是 ...

  6. lucene LZ4 会将doc存储在一个chunk里进行Lz4压缩 ES的_source便如此

    默认情况下,Elasticsearch 用 JSON 字符串来表示文档主体保存在 _source 字段中.像其他保存的字段一样,_source 字段也会在写入硬盘前压缩.The _source is ...

  7. WAF——针对Web应用发起的攻击,包括但不限于以下攻击类型:SQL注入、XSS跨站、Webshell上传、命令注入、非法HTTP协议请求、非授权文件访问等

    核心概念 WAF Web应用防火墙(Web Application Firewall),简称WAF. Web攻击 针对Web应用发起的攻击,包括但不限于以下攻击类型:SQL注入.XSS跨站.Websh ...

  8. 远程登录工具 —— filezilla(FTP vs. SFTP)、xshell、secureCRT

    filezilla:是一个免费开源的 FTP 软件,分为客户端版本和服务器版本,具备所有的 FTP 软件功能. 支持的协议:FTP & SFTP(Secure File Transfer Pr ...

  9. canvas指定的宽高写在行间和写在style里面的区别?

    上代码,指定的canvas宽高都一样,线条的粗细都是5px 1.宽:400:高:300:直接写在<canvas>里的效果: 2.删除<canvas>里的宽高,宽:400:高:3 ...

  10. vue2.0 兄弟组件数据传递方法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...