关于Cookie跨域的问题

来源:互联网 发布:淘宝网不显示惠惠比价 编辑:程序博客网 时间:2024/06/05 13:31

转自:http://www.admin10000.com/document/291.html

Cookie是一个伟大的发明,它允许Web开发者保留他们的用户的登录状态。但是当你的站点有一个以上的域名时就会出现问题了。在Cookie规范上说,一个cookie只能用于一个域名,不能够发给其它的域名。因此,如果在浏览器中对一个域名设置了一个cookie,这个cookie对于其它的域名将无效。如果你想让你的用户从你的站点中的其中一个进行登录,同时也可以在其它域名上进行登录,这可真是一个大难题。

跨二级域名

  我们知道cookie是可以跨二级域名来访问,这个很好理解,例如你 www.test1.com 在的web应用程序创建了一个cookie,要想在bbs.test1.com这样的二级域名对应的应用程序中访问,就必须你在创建cookie的时候设置domain参数domain=test1.com。 以asp.net为例 代码如下:

1
2
3
4
HttpCookie cookie = <span id="12_nwp"style="width: auto; height: auto; float: none;"><a id="12_nwl"href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=20&is_app=0&jk=45e7f593cc2643c9&k=new&k0=new&kdi0=0&luki=1&mcpm=0&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=c94326cc93f5e745&ssp2=1&stid=9&t=tpclicked3_hc&td=1922429&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F291%2Ehtml&urlid=0"target="_blank"mpid="12"style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">new</span></a></span> HttpCookie("name","www.Admin10000.com");
cookie.Domain = "test1.com";
cookie.Path = "/";
Response.Cookies.Add(cookie);

跨顶级域名

  如果我不是二级域名而是完全在不同顶级域名中,例如 www.test1.com 所在的web应用程序创建了一个cookie,想要在 www.test2.com 或其二级域名的应用程序中访问,改怎么办呢?我们知道靠常规反的方法是访问不了的,关键我们就是看看有没有方法可以访问。事实是Cookie可以在一定条件下跨域,而不是随心所欲的实现跨域。

  我们来做个测试,看看两个站点 www.test1.com 和 www.test2.com 如何实现cookie跨域访问。 按照常规我们需要有2个顶级域名,并且有DNS服务器才能够配置域名,否则我们是无法验证的,但是这里我们也没有必要那么麻烦,我们可以通过修改hosts文件来模拟。在 c:\windows\system32\drivers\etc 中有 hosts文件,在末尾添加上

127.0.0.1    www.test1.com127.0.0.1    www.test2.com 

两行,就可以将本机用上面的域名访问本机回环地址了。我们只需要在IIS上部署一套程序,ip为本机回环地址,用两个域名分别访问就可以了。

  我们新建三个页面,分别是 Default.aspx、SSO.ashx、GetCookie.aspx。

  其中Default.aspx是 www.test1.com 的页面,访问的地址是 http://www.test1.com/Default.aspx。看一下前台代码,它没有任何后台代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Admin10000.Web.Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<<spanid="6_nwp"style="width: auto; height: auto; float: none;"><aid="6_nwl"href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=20&is_app=0&jk=45e7f593cc2643c9&k=head&k0=head&kdi0=0&luki=8&mcpm=0&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=c94326cc93f5e745&ssp2=1&stid=9&t=tpclicked3_hc&td=1922429&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F291%2Ehtml&urlid=0"target="_blank"mpid="6"style="text-decoration: none;"><spanstyle="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">head</span></a></span> runat="server">
    <title></title>
</head>
<body>
    <formid="form1"runat="server">
    <div>
 
        <script<span id="7_nwp"style="width: auto; height: auto; float: none;"><aid="7_nwl"href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=20&is_app=0&jk=45e7f593cc2643c9&k=type&k0=type&kdi0=0&luki=4&mcpm=0&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=c94326cc93f5e745&ssp2=1&stid=9&t=tpclicked3_hc&td=1922429&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F291%2Ehtml&urlid=0"target="_blank"mpid="7"style="text-decoration: none;"><spanstyle="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">type</span></a></span>="text/<spanid="8_nwp"style="width: auto; height: auto; float: none;"><aid="8_nwl"href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=20&is_app=0&jk=45e7f593cc2643c9&k=javascript&k0=javascript&kdi0=0&luki=2&mcpm=0&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=c94326cc93f5e745&ssp2=1&stid=9&t=tpclicked3_hc&td=1922429&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F291%2Ehtml&urlid=0"target="_blank"mpid="8"style="text-decoration: none;"><spanstyle="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">javascript</span></a></span>">
            var _<spanid="9_nwp"style="width: auto; height: auto; float: none;"><aid="9_nwl"href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=20&is_app=0&jk=45e7f593cc2643c9&k=frm&k0=frm&kdi0=0&luki=3&mcpm=0&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=c94326cc93f5e745&ssp2=1&stid=9&t=tpclicked3_hc&td=1922429&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F291%2Ehtml&urlid=0"target="_blank"mpid="9"style="text-decoration: none;"><spanstyle="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">frm</span></a></span> = document.createElement("iframe");
            _frm.style.display = "none";
            _frm.src = "http://www.test2.com/SSO.ashx";
            document.body.appendChild(_frm);  
        </script>
 
    </div>
    </form>
</body>
</html>

  另外一个是 SSO.ashx 页面,我们认为它是 www.test2.com 的页面,前台没有任何代码,后台代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.Services;
usingSystem.Web.SessionState;
 
namespaceAdmin10000.Web
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    publicclass SSO : IHttpHandler
    {
 
        publicvoid ProcessRequest(HttpContext context)
        {
            HttpCookie cookie = <span id="4_nwp"style="width: auto; height: auto; float: none;"><a id="4_nwl"href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=20&is_app=0&jk=45e7f593cc2643c9&k=new&k0=new&kdi0=0&luki=1&mcpm=0&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=c94326cc93f5e745&ssp2=1&stid=9&t=tpclicked3_hc&td=1922429&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F291%2Ehtml&urlid=0"target="_blank"mpid="4"style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">new</span></a></span> HttpCookie("name","www.Admin10000.com");
            cookie.Domain = "test2.com";
            cookie.Path = "/";
            cookie.Expires = DateTime.Now.AddMinutes(10000);
            context.Response.Cookies.Add(cookie);
 
            context.Response.ContentType = "text/plain";
            context.Response.AddHeader("P3P","CP=CAO PSA OUR");
            context.Response.Write("");
        }
 
        publicbool IsReusable
        {
            get
            {
                returnfalse;
            }
        }
    }
}

  最后是 GetCookie.aspx 页面,它同样是www.test2.com下的页面,没有前台代码,只有后台代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
 
namespaceAdmin10000.Web
{
    publicpartial class GetCookie : System.Web.UI.Page
    {
        protectedvoid Page_Load(objectsender, EventArgs e)
        {
            if(Request.Cookies["name"] != null)
            {
                Response.Write(Request.Cookies["name"].Value);
            }
        }
    }
}

  好了,现在我们访问测试,通过访问 http://www.test1.com/Default.aspx 之后,这时会通过iframe载入调用SSO.ashx这个页面,执行后台代码创建cookie,然后访问 http://www.test2.com/GetCookie.aspx  我们得到了相应的cookie。说明在www.test1.com下创建的cookie在www.test2.com下是可以访问到的。

要注意的地方:

  admin10000.com 提示 SSO.ashx 的后台代码中有一句:context.Response.AddHeader("P3P", "CP=CAO PSA OUR"); 是用来设置P3P响应头。是因为IE浏览器支持的P3P导致iframe跨站点时cookie被阻止,无法创建cookie。(FireFox目前还不支持P3P安全特性,FireFox自然也不存在此问题。不需要添加P3P响应头。)

  通过iframe的src属性将test1.com域下的cookie值作为get参数重定向到test2.com域下SSO.ashx页面上,SSO.ashx获取test1.com域中所传过来的cookie值,并将所获取到值写入cookie中,这样就简单的实现了cookie跨域的访问。

  另外Default.aspx页面也可改为JS调用形式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Admin10000.Web.Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<htmlxmlns="http://www.w3.org/1999/xhtml">
<<spanid="0_nwp"style="width: auto; height: auto; float: none;"><aid="0_nwl"href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=20&is_app=0&jk=45e7f593cc2643c9&k=head&k0=head&kdi0=0&luki=8&mcpm=0&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=c94326cc93f5e745&ssp2=1&stid=9&t=tpclicked3_hc&td=1922429&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F291%2Ehtml&urlid=0"target="_blank"mpid="0"style="text-decoration: none;"><spanstyle="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">head</span></a></span> runat="server">
    <title></title>
</head>
<body>
    <formid="form1"runat="server">
    <div>
        <script<span id="1_nwp"style="width: auto; height: auto; float: none;"><aid="1_nwl"href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=20&is_app=0&jk=45e7f593cc2643c9&k=type&k0=type&kdi0=0&luki=4&mcpm=0&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=c94326cc93f5e745&ssp2=1&stid=9&t=tpclicked3_hc&td=1922429&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F291%2Ehtml&urlid=0"target="_blank"mpid="1"style="text-decoration: none;"><spanstyle="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">type</span></a></span>="text/<spanid="2_nwp"style="width: auto; height: auto; float: none;"><aid="2_nwl"href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=20&is_app=0&jk=45e7f593cc2643c9&k=javascript&k0=javascript&kdi0=0&luki=2&mcpm=0&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=c94326cc93f5e745&ssp2=1&stid=9&t=tpclicked3_hc&td=1922429&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F291%2Ehtml&urlid=0"target="_blank"mpid="2"style="text-decoration: none;"><spanstyle="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">javascript</span></a></span>" src="http://www.test2.com/SSO.ashx"></script>
    </div>
    </form>
</body>
</html>
QQ群: WEB开发者官方总群(515171538) 验证消息:Admin10000


0 0
原创粉丝点击