关于Cookie跨域的问题
来源:互联网 发布:方舟史上最强优化 编辑:程序博客网 时间:2024/05/19 15:22
跨二级域名
我们知道cookie是可以跨二级域名来访问,这个很好理解,例如你 www.test1.com 在的web应用程序创建了一个cookie,要想在bbs.test1.com这样的二级域名对应的应用程序中访问,就必须你在创建cookie的时候设置domain参数domain=test1.com。 以asp.net为例 代码如下:
- HttpCookie cookie = new 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。看一下前台代码,它没有任何后台代码
- <%@ 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">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <script type="text/javascript">
- var _frm = 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 的页面,前台没有任何代码,后台代码如下:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Services;
- using System.Web.SessionState;
- namespace Admin10000.Web
- {
- /// <summary>
- /// $codebehindclassname$ 的摘要说明
- /// </summary>
- [WebService(Namespace = "http://tempuri.org/")]
- [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
- public class SSO : IHttpHandler
- {
- public void ProcessRequest(HttpContext context)
- {
- HttpCookie cookie = new 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("");
- }
- public bool IsReusable
- {
- get
- {
- return false;
- }
- }
- }
- }
最后是 GetCookie.aspx 页面,它同样是www.test2.com下的页面,没有前台代码,只有后台代码:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace Admin10000.Web
- {
- public partial class GetCookie : System.Web.UI.Page
- {
- protected void Page_Load(object sender, 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调用形式:
- <%@ 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">
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <script type="text/javascript" src="http://www.test2.com/SSO.ashx"></script>
- </div>
- </form>
- </body>
- </html>
- 关于Cookie跨域的问题
- 关于Cookie跨域的问题
- 关于Cookie跨域的问题
- 关于Cookie跨域的问题
- 关于Cookie跨域的问题
- 关于Cookie跨域操作遇到的问题及解决方法
- 关于Cookie跨域操作遇到的问题及解决方法
- 关于跨域获取cookie问题的解决
- 关于cookie 的过期问题
- 关于cookie的一些问题
- cookie跨域的问题
- 关于cookie的跨域问题与XSS的原理与防护
- 关于Cookie的路径及Cookie的获取问题?
- php中cookie技术关于跨目录调用cookie值的问题
- 关于Asp.net中Cookie的问题
- 关于二级域名Cookie的问题及解决方法
- 关于Asp.net中Cookie的问题
- 关于二级域名Cookie的问题及解决方法
- JQuery中阻止事件冒泡方式及其区别
- 将引用做为传递参数
- linux socket中关闭连接
- java 泛型详解
- 如何从 Visual Basic 的另一个用户身份启动进程
- 关于Cookie跨域的问题
- Effective stl 第6条
- FlowLayout布局管理器
- 进程_控制进程的资源使用
- GCD使用(一)执行与UI有关的操作
- Java程序员面试中的多线程问题
- 51单片机学习点滴记录
- php中strstr、strrchr、substr、stristr四个函数用法区别
- ArcGIS API for Javascript 应用开发入门