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

推荐订阅源

MyScale Blog
MyScale Blog
量子位
宝玉的分享
宝玉的分享
爱范儿
爱范儿
云风的 BLOG
云风的 BLOG
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Recent Announcements
Recent Announcements
Apple Machine Learning Research
Apple Machine Learning Research
N
News and Events Feed by Topic
TaoSecurity Blog
TaoSecurity Blog
博客园 - 三生石上(FineUI控件)
小众软件
小众软件
Simon Willison's Weblog
Simon Willison's Weblog
Google DeepMind News
Google DeepMind News
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
aimingoo的专栏
aimingoo的专栏
Cloudbric
Cloudbric
Blog — PlanetScale
Blog — PlanetScale
Latest news
Latest news
S
Security @ Cisco Blogs
Last Week in AI
Last Week in AI
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Vercel News
Vercel News
W
WeLiveSecurity
M
MIT News - Artificial intelligence
P
Proofpoint News Feed
P
Proofpoint News Feed
P
Palo Alto Networks Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
The Blog of Author Tim Ferriss
腾讯CDC
大猫的无限游戏
大猫的无限游戏
Martin Fowler
Martin Fowler
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
V2EX
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Stack Overflow Blog
Stack Overflow Blog
IT之家
IT之家
有赞技术团队
有赞技术团队
Microsoft Security Blog
Microsoft Security Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
美团技术团队
博客园 - 【当耐特】
D
DataBreaches.Net
I
InfoQ
G
GRAHAM CLULEY
S
SegmentFault 最新的问题
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog

博客园 - dragonpig

Html 5 Canvas绘制分形图Mandelbrot .net中反射、emit、expression和dynamic的性能比较 const string 和 static readonly string的区别 SqlServer: Top N per Group 微软的BinarySearch 通过CTE实现Split CSV 通过SQL CTE计算Fibonacci 当json.js遇见dynamic.net烂尾篇 .NET线程安全泛型Singleton WCF JSON和AspnetCompatibility的配置 Windows安装Memcached node.js初体验 教你如何制作Silverlight Visual Tree Inspector 一道非常有趣的概率题 教你30秒打造强类型ASP.NET数据绑定 当json.js遇见dynamic.net [0] 用Silverlight做雷达图 C#运算符重载不是没有用武之地 随机排列算法
跨域访问Cookie
dragonpig · 2011-02-27 · via 博客园 - dragonpig

通过jQuery.get是不能异步访问跨域资源的。主要是因为安全考虑,否则其他域有可能获得当前页的cookie造成隐私泄漏。但js确可以跨域访问,因为所有浏览器都支持refer外部的js文件。访问外部域的js时会发送外部域的cookie,这样在返回的js中就能获取值了。

创建两个website,分别叫local.domain1.com和local.domain2.com

domain1中负责set和get本域的cookie,分别为setcookie.aspx和getcookie.aspx

//setcookie.aspx
public partial class SetCookie : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Headers.Add(
"P3P",
@"CP=""CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR""");if (Response.Cookies != null && Request.Cookies.AllKeys.Contains("password"))
Response.Write(Request.Cookies[
"password"].Value);
HttpCookie aCookie
= new HttpCookie("password");
aCookie.Value
= "secret1!";
aCookie.Expires
= DateTime.Now.AddDays(1);
Response.SetCookie(aCookie);
}
}
//getcookie.aspx
public partial class GetCookie : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string val = string.Empty;
if (Request.Cookies != null && Request.Cookies.AllKeys.Contains("password"))
val
= (Request.Cookies["password"].Value);
Response.Write(Request.QueryString[
"callback"] + "('" + val + "')");
}
}

setcookie.aspx => 存放password=secret1!的cookie,这存在潜在的危险,一开始的header是为了允许ie修改cookie

getcookie.aspx => 返回jsonp数据,其中的callback是由jquery自动生成的

在domain2中,添加访问页面getcrossdomaincookie.aspx

<script src="http://code.jquery.com/jquery-1.5.1.min.js" type="text/javascript"></script>
<script>
$(function () {
$.getJSON('http://local.domain1.com/getcookie.aspx?callback=?', function (data) {
alert(data);
});
});
</script>

关键在于callback=?,其中?说明由jquery自动生成jsonp的callback方法。

其实getJSON的设计有些tricky,包含了两种不同的行为。如果没有callback=?,就是普通的异步访问本域的资源然后返回结果。但如果是jsonp的形式,事实上会通过在header tag中动态加入script tag然后在载入后删除(IE似乎没有删除),当然这也是jsonp的标准做法。