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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - flyfish

Microsoft SQL Server 2005 -- 错误 29503。SQL Server 服务无法启动 模拟鼠标移动和左键单击 .C# 获取另一程序控件,改变值,触发事件 - flyfish - 博客园 在ASP程序中调用Web Service ASPNET2.0中读写Cookie的方法! - flyfish - 博客园 ASP.NET WAP开发 用ASP.NET创建移动Web窗体[转] ASP.NET 2.0移动开发入门之使用模拟器 [WAP]dotNet在WAP应用开发中实现按指定页数翻页的解决方案 SQL Server 自增字段归零等问题 ntext replace sql SQLServer2005数据库还原到SQLServer2000 如何去除Google搜索结果病毒提示 Tomcat 6 连接 MS SQL 2005 Windows 2003远程桌面连接数限制 如何用SQL命令修改字段名称 FCKeditor详细的设置 log4net 配置与应用 两个sql server 2000的通用分页存储过程
转 跨域读取Cookie和session之HttpWebRequest另类方法(网站API开发)
flyfish · 2009-07-14 · via 博客园 - flyfish

在网上找了很多跨域读取Cookie的方法,但都是A域主动设置B域的Cookie,而没有B域去获取A域Cookie的方法。

若要转载,请注名出处 http://blog.csdn.net/try530/archive/2009/01/06/3721525.aspx

所谓A域主动设置B域的Cookie

1:在B.com上新建一文件:SetCookie.aspx

 protected void Page_Load(object sender, EventArgs e)
        {
            HttpContext.Current.Response.AddHeader("p3p", "CP=CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR");
            HttpCookie cookie = new HttpCookie("userid", "44");
            cookie.Domain = ".b.com";

                        //  cookie.Expires = DateTime.Now.AddSeconds((double)expires);
            HttpContext.Current.Response.AppendCookie(cookie);
        }
2:在A域新建一文件:Default.aspx,在前台页面调用B域的SetCookie.aspx页面,来为B域设置相应的Cookie.
    <script src="http://www.b.com/SetCookie.aspx%22%3E%3C/script>
3:在B域新建一文件:Default.aspx来显示被A域设置的Cookie。

 protected void Page_Load(object sender, EventArgs e)
    {

               Response.Write(Request.Cookies["userid"] == null ? "" : Request.Cookies["userid"].Value.ToString());

    }
4:以此访问http://www.a.com/default.aspx----%3Ewww.b.com/default.aspx

以上为A域主动设置B域的Cookie,适用于单点登录,但必须在B域,C域或D域上新建setcookie.aspx文件来让A域帮忙设置Cookie。

那B域C域或D域如何根据自身的需要去主动获取A域的Cookie呢?请看以下方法,以下方法为此文重点。

1:在A域新建一文件:SetCookie.aspx,此文件用来设置A域自己的Cookie。

   protected void Page_Load(object sender, EventArgs e)
        {
            HttpCookie cookie = new HttpCookie("userid", "44");
            HttpContext.Current.Response.AppendCookie(cookie);
        }
2:A域的Cookie设置完了,那怎么让其他域来读取自己的Cookie呢,这就是重点了。
新建一页面:OpenID.aspx,用来让B域读取Cookie,并自动设置B域的cookie。(这里有点昏)  

    protected void Page_Load(object sender, EventArgs e)
    {
        HttpContext.Current.Response.AddHeader("p3p", "CP=CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR");
        if (Request.Cookies["userid"] != null)
        {
            Response.Write(@"
            var userid=" + Request.Cookies["userid"].Value.ToString() + @";
            SetCookie('userid',userid);//什么域调用此文件,设置的Cookie将是什么域的。
            window.location.href=document.URL;

                 function SetCookie(name,value)//两个参数,一个是cookie的名子,一个是值
                {
                   document.cookie = name + ""=""+ escape (value) ;
                 }
            ");
        }
        else
        {
            Response.Write("document.write('没有cookie');");
        }
    }


3:A域的工作已经完了,那B域如何得到A域的这个Cookie值呢,在B域新建一页:default.aspx

 protected void Page_Load(object sender, EventArgs e)
        {
           if (HttpContext.Current.Request.Cookies["userid"] != null)
            {
                Label1.Text = HttpContext.Current.Request.Cookies["userid"].Value.ToString();

            }
            else
            {

4:依次访问 www.a.com/setcookie.aspx ------> www.b.com/default.aspx    此方法用于A域开发API给其他域调用。主要用于A域API接口的开发。就像现在的SNS网站提供当前登录的用户信息给其他应用程序(如抢车位)一样,不过我不知道他们是怎么实现的,但我用此方法实现了。
总结下此方法的步骤:
1:A域设置cookie
2:B域用调用javascript脚本的方式读取A域cookie,读取后,并同步设置B域Cookie.
3:调用B域时,发现没有cookie就自动从A域获取cookie值来设置自己的Cookie。
A域可以把怎么读取的方法做成dll,提供给其他域。这称之为A域的API。

测试成功,用此方法能跨域读取session,方式是:把A域的Session值存入B域的cookie中。至于怎么存,存在什么cookie名称里,由A域提供。


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/try530/archive/2009/01/06/3721525.aspx