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

推荐订阅源

W
WeLiveSecurity
博客园 - 【当耐特】
Microsoft Azure Blog
Microsoft Azure Blog
WordPress大学
WordPress大学
Stack Overflow Blog
Stack Overflow Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
IT之家
IT之家
Cloudbric
Cloudbric
The Register - Security
The Register - Security
小众软件
小众软件
PCI Perspectives
PCI Perspectives
G
Google Developers Blog
AI
AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Google DeepMind News
Google DeepMind News
Google DeepMind News
Google DeepMind News
宝玉的分享
宝玉的分享
Recent Commits to openclaw:main
Recent Commits to openclaw:main
量子位
TaoSecurity Blog
TaoSecurity Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
F
Full Disclosure
N
Netflix TechBlog - Medium
博客园_首页
Last Week in AI
Last Week in AI
A
Arctic Wolf
B
Blog RSS Feed
J
Java Code Geeks
C
Cybersecurity and Infrastructure Security Agency CISA
I
InfoQ
aimingoo的专栏
aimingoo的专栏
云风的 BLOG
云风的 BLOG
NISL@THU
NISL@THU
MyScale Blog
MyScale Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Jina AI
Jina AI
有赞技术团队
有赞技术团队
S
Schneier on Security
L
Lohrmann on Cybersecurity
P
Privacy & Cybersecurity Law Blog
T
Threat Research - Cisco Blogs
P
Palo Alto Networks Blog
S
Security @ Cisco Blogs
Security Archives - TechRepublic
Security Archives - TechRepublic
Security Latest
Security Latest
Vercel News
Vercel News
博客园 - 司徒正美
Webroot Blog
Webroot Blog
Hacker News: Ask HN
Hacker News: Ask HN
A
About on SuperTechFans

博客园 - 俩醒叁醉

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); } } }