问题

我打开了 www.aaa.com 里面的一个页面(www.aaa.com/hello.php),其中这个 hello.php 里面包含一个 <img> 标签,里面的 src 来自于 www.bbb.com/get.php。
请问如果我已经在 aaa.com 里面登录了,那么访问 hello.php 时会不会把 cookie 发送给 www.bbb.com/get.php ?

实战

没有什么比实战测试更有说服力了!

新建 2 个独立的站点 A,B  , 分别对应域名 aaa.com , bbb.com,其中在 A 里面制作一个登录功能,并且还有一直图片引用 bbb.com,登录后 aaa.com 会写入 cookie 到客户端,观察登录前后 bbb.com 是否能获取到 aaa.com 里面的 cookie。

1. A 站点的前台代码:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="CrossSiteCookieDemo_SiteA.WebUI.index" %>
  2.  
  3. <!DOCTYPE html>
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml">
  6. <head runat="server">
  7. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  8. <title></title>
  9. </head>
  10. <body>
  11. <form id="form1" runat="server">
  12. <div>
  13. <h1>跨域名 Cookie 传递测试</h1>
  14. </div>
  15. <br /><br />
  16. <asp:Panel ID="pnLogin" runat="server" Visible="true">
  17. 用户名:<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
  18. &nbsp;&nbsp;&nbsp;
  19. 密码:<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
  20. &nbsp;&nbsp;&nbsp;
  21. <asp:Button ID="btnLogin" runat="server" Text="登录" OnClick="btnLogin_Click"/>
  22. </asp:Panel>
  23.  
  24. <asp:Panel ID="pnWelcomeInfo" runat="server" Visible="false">
  25. 欢迎您,<asp:Literal ID="ltShowUserName" runat="server" EnableViewState="false"/>
  26. &nbsp;&nbsp;&nbsp;
  27. <asp:Button ID="btnLogout" runat="server" Text="注销" OnClick="btnLogout_Click" />
  28. </asp:Panel>
  29.  
  30. <br /><br />
  31. 图片(这个图片是另一个域名下的图片):
  32. <img alt="这个图片是另一个域名下的图片" title="这个图片是另一个域名下的图片" src="http://www.bbb.com/index.ashx" />
  33. <br /><br />
  34. <div>
  35. <asp:Label ID="lblMessage" runat="server" EnableViewState="false"></asp:Label>
  36. </div>
  37. <br /><br />
  38. </form>
  39. </body>
  40. </html>

2. A 站点的后台代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7.  
  8. namespace CrossSiteCookieDemo_SiteA.WebUI
  9. {
  10. public partial class index : System.Web.UI.Page
  11. {
  12. protected void Page_Load(object sender, EventArgs e)
  13. {
  14. if(!IsPostBack)
  15. {
  16. InitData();
  17. }
  18. }
  19.  
  20. protected void InitData()
  21. {
  22. HttpCookie accountCookie = Request.Cookies["root_account_info"];
  23. if (accountCookie != null)
  24. {
  25. this.pnWelcomeInfo.Visible = true;
  26. this.pnLogin.Visible = false;
  27. this.ltShowUserName.Text = HttpUtility.UrlDecode(accountCookie["username"]);
  28. }
  29. }
  30.  
  31. protected void btnLogin_Click(object sender, EventArgs e)
  32. {
  33. string userName = this.txtUserName.Text.Trim();
  34. string password = this.txtPassword.Text.Trim();
  35.  
  36. if (string.IsNullOrEmpty(userName))
  37. {
  38. this.lblMessage.Text = "用户名不能为空!";
  39. this.lblMessage.ForeColor = System.Drawing.Color.Red;
  40. return;
  41. }
  42. if (string.IsNullOrEmpty(password))
  43. {
  44. this.lblMessage.Text = "密码不能为空!";
  45. this.lblMessage.ForeColor = System.Drawing.Color.Red;
  46. return;
  47. }
  48. HttpCookie accountCookie = new HttpCookie("root_account_info");
  49. accountCookie.Expires = DateTime.Now.AddDays(1d);
  50. accountCookie["username"] = HttpUtility.UrlEncode(userName); // 仅仅为了测试,省略了加密
  51. accountCookie["password"] = HttpUtility.UrlEncode(password); // 仅仅为了测试,省略了加密
  52.  
  53. Response.AppendCookie(accountCookie);
  54.  
  55. HttpCookie loginStatCookie = Request.Cookies["root_login_stat_info"];
  56. if (loginStatCookie == null)
  57. {
  58. loginStatCookie = new HttpCookie("root_login_stat_info");
  59. loginStatCookie.Expires = DateTime.Now.AddYears();
  60. loginStatCookie["firstLoginTime"] = HttpUtility.UrlEncode(DateTime.Now.ToString("yyyyMMddHHmmss")); // 仅仅为了测试,省略了加密
  61. loginStatCookie["LoginCount"] = "";
  62. }
  63. else
  64. {
  65. string loginCountStr = loginStatCookie["LoginCount"];
  66. int loginCount;
  67. int.TryParse(loginCountStr, out loginCount);
  68. loginCount++;
  69. loginStatCookie["LoginCount"] = loginCount.ToString();
  70. }
  71. loginStatCookie["lastLoginTime"] = HttpUtility.UrlEncode(DateTime.Now.ToString("yyyyMMddHHmmss")); // 仅仅为了测试,省略了加密
  72.  
  73. Response.AppendCookie(loginStatCookie);
  74.  
  75. this.pnWelcomeInfo.Visible = true;
  76. this.pnLogin.Visible = false;
  77. this.ltShowUserName.Text = userName;
  78. }
  79.  
  80. protected void btnLogout_Click(object sender, EventArgs e)
  81. {
  82. HttpCookie accountCookie = Request.Cookies["root_account_info"];
  83. if (accountCookie != null)
  84. {
  85. accountCookie.Expires = DateTime.Now.AddYears(-);
  86. Response.AppendCookie(accountCookie);
  87. }
  88. this.pnWelcomeInfo.Visible = false;
  89. this.pnLogin.Visible = true;
  90. this.ltShowUserName.Text = string.Empty;
  91. }
  92. }
  93. }

3. B 站点的处理程序代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Text;
  6.  
  7. namespace CrossSiteCookieDemo_SiteB.WebUI
  8. {
  9. /// <summary>
  10. /// index 的摘要说明
  11. /// </summary>
  12. public class index : IHttpHandler
  13. {
  14.  
  15. public void ProcessRequest(HttpContext context)
  16. {
  17. string cookieContent = new string('=', ) + "时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + new string('=', ) + "\r\n\r\n";
  18. StringBuilder sbAppend = new StringBuilder();
  19. foreach (HttpCookie cookieItem in context.Request.Cookies)
  20. {
  21. sbAppend.Append("cookieName: " + cookieItem.Name + "\r\n");
  22. sbAppend.Append("cookieValue: \r\n");
  23. if(cookieItem.Values != null && cookieItem.Values.AllKeys != null && cookieItem.Values.AllKeys.Length > )
  24. {
  25. foreach (string childCookieKey in cookieItem.Values.AllKeys)
  26. {
  27. sbAppend.AppendFormat(new string(' ', ) + "{0}: {1} \r\n", childCookieKey, cookieItem.Values[childCookieKey]);
  28. }
  29. }
  30. }
  31. cookieContent += sbAppend.ToString();
  32. System.IO.File.AppendAllText(System.Web.Hosting.HostingEnvironment.MapPath("~/log/cookie.log"), cookieContent, System.Text.Encoding.UTF8);
  33.  
  34. context.Response.ContentType = "image/jpeg";
  35. context.Response.TransmitFile(System.Web.Hosting.HostingEnvironment.MapPath("~/images/01.jpg"));
  36. }
  37.  
  38. public bool IsReusable
  39. {
  40. get
  41. {
  42. return false;
  43. }
  44. }
  45. }
  46. }

4. 运行截图:

01

02

03

04

05

结论:跨域 Cookie 是无法传递的,浏览器会阻止!

谢谢浏览!

跨域名 Cookie 传递测试的更多相关文章

  1. CORS跨域cookie传递

    服务端 Access-Control-Allow-Credentials:true Access-Control-Allow-Methods:* Access-Control-Allow-Origin ...

  2. CORS跨域、Cookie传递SessionID实现单点登录后的权限认证的移动端兼容性测试报告

    简述 本文仅记录如标题所述场景的测试所得,由于场景有些特殊,且并不需兼容所有浏览器,所以本文的内容对读者也许并无作用,仅为记录. 场景.与实现 需在移动端单点登录 需在移动端跨域访问我们的服务 基于历 ...

  3. 跨域请求传递Cookie问题

    问题描述 前后端完全分离的项目,前端使用Vue + axios,后端使用SpringMVC,容器为Tomcat. 使用CORS协议解决跨域访问数据限制的问题,但是发现客户端的Ajax请求不会自动带上服 ...

  4. cookie的使用以及cookie的跨域名获取

    cookie存放容量4k左右,可设置过期时间. 1.cookie的封装使用 //设置cookies function setCookie(name, value) { var Days = 30; v ...

  5. 关于vue跨域名对接微信授权认证和APP授权认证

    这种情况一般也只会出现在前后端分离,跨域名授权的时候吧.耗费了一个前端+一个后台+一个网关,熬夜通宵了两天才整出来一套方法(你们见过凌晨6点的杭州吗,对,我下班的时候天黑了,到家天亮了....),和开 ...

  6. iframe跨域数据传递

    项目中需要和其他单位合作开发,方案采用iframe嵌入页面,开发过程中设计到了跨域数据的传递,初步方案决定使用html5 API postMessage进行iframe跨域数据传递: 域名A下的页面 ...

  7. Jmeter(五十二) - 从入门到精通高级篇 - jmeter之跨线程组传递参数(详解教程)

    1.简介 之前分享的所有文章都是只有一个线程组,而且参数的传递也只在一个线程组中,那么如果需要在两个线程组中传递参数,我们怎么做呢?宏哥今天就给小伙伴或者童鞋们讲解一下,如何实现在线程组之间传递参数. ...

  8. JavaScript创建读取cookie代码示例【附:跨域cookie解决办法】

    使用JavaScript 原生存取cookie代码示例: var cookie = { set : function(name, value, expires, path, domain, secur ...

  9. session跨域和ajax跨域名

    后台跨域和ajax跨域名: 后台跨域: www.baidu.com   主域名(一级域名一般以www开头) news.baidu.com   二级域名 (a.test.com和b.test.com有相 ...

随机推荐

  1. paip.spring3 mvc servlet的配置以及使用最佳实践

    paip.spring3 mvc servlet的配置以及使用最佳实践 1. Web.xml 1 2. springMVC.xml 2 1. mvcAction .mvcAction 2 2. Res ...

  2. paip.注册java程序为LINUX系统服务的总结。

    paip.注册java程序为LINUX系统服务的总结. ////////////////实现开机启动. 标准方法是按照/etc/init.d/下面的文件,修改一下:然后chkconfig xxx on ...

  3. paip.基于urlrewrite的反向代理以及内容改写

    paip.基于urlrewrite的反向代理以及内容改写 ---------反向代理 RewriteCond %{REQUEST_URI} !=/process.php RewriteRule  ^( ...

  4. sqlserver数据库维护脚本大全,值得收藏

    下面的代码非但有图文,简直是视频,地址http://www.cnthc.com/?/article/67http://www.cnthc.com/?/article/73 --创建一个玩的数据库Cre ...

  5. python coroutine测试

    目的:实现一个类似于asyn await的用法,来方便的编写callback相关函数 from __future__ import print_functionimport timeimport th ...

  6. Distributed Result Grouping Caveats

    Distributed Result Grouping Caveats Grouping is supported distributed searches, with some caveats: 1 ...

  7. 如何判断平台工具集去做条件编译(VC++目录、预处理器定义、$(PlatformToolsetVersion))

    作者:zyl910 从VS2010开始,提供了一个平台工作集(Platform ToolSet)选项用于配制vc编译版本.到了VS2012,更是因为默认平台工具集不支持WindowsXP,导致经常需要 ...

  8. Objective C for Windows

    You can use Objective C inside the Windows environment. If you follow these steps, it should be work ...

  9. 机器学习基石--学习笔记01--linear hard SVM

    背景 支持向量机(SVM)背后的数学知识比较复杂,之前尝试过在网上搜索一些资料自学,但是效果不佳.所以,在我的数据挖掘工具箱中,一直不会使用SVM这个利器.最近,台大林轩田老师在Coursera上的机 ...

  10. Linux 输出文件列数,拼接文件

    如果我只想看看文件的前几行,每行的字段数(列数),我的文件已tab作为分隔符(这个可以自己指定),其具体命令如下: head fileName | awk -F'\t' '{print NF}' 如果 ...