惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
Recent Announcements
Recent Announcements
D
Docker
V
V2EX
阮一峰的网络日志
阮一峰的网络日志
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
The GitHub Blog
The GitHub Blog
U
Unit 42
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
月光博客
月光博客
腾讯CDC
B
Blog
博客园_首页
罗磊的独立博客
D
DataBreaches.Net
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
L
LangChain Blog
aimingoo的专栏
aimingoo的专栏
MongoDB | Blog
MongoDB | Blog
GbyAI
GbyAI
Stack Overflow Blog
Stack Overflow Blog
M
MIT News - Artificial intelligence

博客园 - 俩醒叁醉

ASP.NET MVC 4 WebAPI. Support Areas in HttpControllerSelector SQL2000安装问题(转) sql server 2008安装需要一直重启。但重启后又没有达到效果。 为数据库中所有的用户数据表生成分页存储过程 SQL 2005 字段备注获取 __doPostBack方法解析 如何在三个月掌握三年的经验(转载&&笔记) jQuery Ajax的使用 JQuery资源网站 深入分析跨域cookie的问题 CnBlogsDotText使用实例 轻松搭建博客平台-开源ASP.NET 博客Subtext 的安装 表达式目录树(源MSDN) Web 2.0 编程思想:16条法则 Control Adapter 以下代码提供查询数据库中是否存在某个值 URL Routing MVC Controllers和Forms验证 CSharp——Lambda 表达式
Cookie跨域、虚拟目录
俩醒叁醉 · 2009-03-15 · via 博客园 - 俩醒叁醉

作者:ITPUB论坛  2008-05-06

Cookie有三个属性需要注意一下:
  1. Domain 域
  2. Path 路径
  3. Expires 过期时间

  跨域操作需要设置域属性:
  Response.Cookies("MyCookie").Domain = "cnblogs.com"; (这里指的是泛域名)
  这样在其它二级域名下就都可以访问到了, ASP 和 ASP.NET 测试通过

  虚拟目录下访问:
  我在ASP端做了下测试,.NET的没试, 如果不指定Path属性, 不同虚拟目录下Cookie无法共享
  将Response.Cookies("MyCookie").Path = "/" 就可以了

  总的写法:

Response.Cookies("MyCookie").Domain = "cnblogs.com"; Response.Cookies("MyCookie").Path = "/" Response.Cookies("MyCookie").Expires = Now + 365; Response.Cookies("MyCookie")("Test") = "test"; .NET 清除Cookie HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies[cookiename]; if (cookie != null) { cookie.Values.Clear(); SetUserCookieExpireTime(cookiename, -1); cookie.Domain = _domain; System.Web.HttpContext.Current.Response.Cookies.Set(cookie); } public static void SetUserCookieExpireTime(string key, int days) { System.Web.HttpContext.Current.Response.Cookies[key].Domain = _domain; System.Web.HttpContext.Current.Response.Cookies[key].Path = _cookiepath; System.Web.HttpContext.Current.Response.Cookies[key].Expires = DateTime.Now.AddDays(days); } .NET 添加/更新Cookie public static void AddUserCookies(string key,string value, string cookiename, string domain) { HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies[cookiename]; if (cookie == null) { cookie = new HttpCookie(cookiename); cookie.Domain = domain; cookie.Path = _cookiepath; cookie.Values.Add(key, value); HttpContext.Current.Response.AppendCookie(cookie); } else { if (System.Web.HttpContext.Current.Request.Cookies[cookiename].Values[key] != null) { cookie.Values.Set(key, value); } else { cookie.Domain = domain; cookie.Path = _cookiepath; cookie.Values.Add(key, value); HttpContext.Current.Response.AppendCookie(cookie); } } }