asp.net關(guān)于Cookie跨域(域名)的問題
跨二級域名
我們知道cookie是可以跨二級域名來訪問,這個很好理解,例如你 www.test1.com 在的web應(yīng)用程序創(chuàng)建了一個cookie,要想在bbs.test1.com這樣的二級域名對應(yīng)的應(yīng)用程序中訪問,就必須你在創(chuàng)建cookie的時候設(shè)置domain參數(shù)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應(yīng)用程序創(chuàng)建了一個cookie,想要在 www.test2.com 或其二級域名的應(yīng)用程序中訪問,改怎么辦呢?我們知道靠常規(guī)反的方法是訪問不了的,關(guān)鍵我們就是看看有沒有方法可以訪問。事實是Cookie可以在一定條件下跨域,而不是隨心所欲的實現(xiàn)跨域。
我們來做個測試,看看兩個站點(diǎn) www.test1.com 和 www.test2.com 如何實現(xiàn)cookie跨域訪問。 按照常規(guī)我們需要有2個頂級域名,并且有DNS服務(wù)器才能夠配置域名,否則我們是無法驗證的,但是這里我們也沒有必要那么麻煩,我們可以通過修改hosts文件來模擬。在 c:\windows\system32\drivers\etc 中有 hosts文件,在末尾添加上
127.0.0.1 www.test1.com
127.0.0.1 www.test2.com
兩行,就可以將本機(jī)用上面的域名訪問本機(jī)回環(huán)地址了。我們只需要在IIS上部署一套程序,ip為本機(jī)回環(huán)地址,用兩個域名分別訪問就可以了。
我們新建三個頁面,分別是 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 頁面,我們認(rèn)為它是 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);
}
}
}
}
好了,現(xiàn)在我們訪問測試,通過訪問 http://www.test1.com/Default.aspx 之后,這時會通過iframe載入調(diào)用SSO.ashx這個頁面,執(zhí)行后臺代碼創(chuàng)建cookie,然后訪問 http://www.test2.com/GetCookie.aspx 我們得到了相應(yīng)的cookie。說明在www.test1.com下創(chuàng)建的cookie在www.test2.com下是可以訪問到的。
要注意的地方:
admin10000.com 提示 SSO.ashx 的后臺代碼中有一句:context.Response.AddHeader("P3P", "CP=CAO PSA OUR"); 是用來設(shè)置P3P響應(yīng)頭。是因為IE瀏覽器支持的P3P導(dǎo)致iframe跨站點(diǎn)時cookie被阻止,無法創(chuàng)建cookie。(FireFox目前還不支持P3P安全特性,F(xiàn)ireFox自然也不存在此問題。不需要添加P3P響應(yīng)頭。)
通過iframe的src屬性將test1.com域下的cookie值作為get參數(shù)重定向到test2.com域下SSO.ashx頁面上,SSO.ashx獲取test1.com域中所傳過來的cookie值,并將所獲取到值寫入cookie中,這樣就簡單的實現(xiàn)了cookie跨域的訪問。
另外Default.aspx頁面也可改為JS調(diào)用形式:
<%@ 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>
相關(guān)文章
asp.net 頁面延時五秒,跳轉(zhuǎn)到另外的頁面
asp.net 頁面延時五秒,跳轉(zhuǎn)到另外的頁面的實現(xiàn)代碼。2009-12-12asp.net 計算字符串中各個字符串出現(xiàn)的次數(shù)
比如一個字符串"a,b,a,c,b,b,d",現(xiàn)在我們要統(tǒng)計每個字符串出現(xiàn)次數(shù)。解決這個問題,我們可以使用泛型集合 Dictionary(TKey,TValue)。它有一個key值用來存儲字符串和一個value值,用來存儲字符串出現(xiàn)的次數(shù)2012-05-05asp.net中Timer無刷新定時器的實現(xiàn)方法
這篇文章主要介紹了asp.net中Timer無刷新定時器的實現(xiàn)方法,是一個非常具有實用價值的技巧,需要用到Ajax技術(shù),需要的朋友可以參考下2014-08-08ASP.NET MVC使用正則表達(dá)式驗證手機(jī)號碼
這篇文章介紹了ASP.NET MVC使用正則表達(dá)式驗證手機(jī)號碼的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-09-09Coolite Cool Study 3 MVC + Coolite 的實現(xiàn)代碼
啊,開始以為MVC+Coolite結(jié)合的例子沒什么難度,但原來Coolite在MVC中需要特定設(shè)置一下某些屬性才行,費(fèi)了兩個小時才算大功告成,具體請看下文。還是先把這個例子的效果貼上來再說。2009-05-05ASP.NET 修復(fù) IIS 映射具體實現(xiàn)步驟
本文主要介紹IIS映射的具體步驟,希望對大家有所幫助。2016-05-05一步步打造簡單的MVC電商網(wǎng)站BooksStore(2)
這篇文章主要和大家一起一步步打造一個簡單的MVC電商網(wǎng)站,MVC電商網(wǎng)站BooksStore第二篇,添加分類導(dǎo)航、加入購物車,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04