主頁 > 知識(shí)庫 > asp.net關(guān)于Cookie跨域(域名)的問題

asp.net關(guān)于Cookie跨域(域名)的問題

熱門標(biāo)簽:銀行業(yè)務(wù) 阿里云 團(tuán)購網(wǎng)站 Mysql連接數(shù)設(shè)置 Linux服務(wù)器 電子圍欄 服務(wù)器配置 科大訊飛語音識(shí)別系統(tǒng)

跨二級(jí)域名
  我們知道cookie是可以跨二級(jí)域名來訪問,這個(gè)很好理解,例如你 www.test1.com 在的web應(yīng)用程序創(chuàng)建了一個(gè)cookie,要想在bbs.test1.com這樣的二級(jí)域名對(duì)應(yīng)的應(yīng)用程序中訪問,就必須你在創(chuàng)建cookie的時(shí)候設(shè)置domain參數(shù)domain=test1.com。 以asp.net為例 代碼如下:

復(fù)制代碼 代碼如下:

HttpCookie cookie = new HttpCookie("name", "www.Admin10000.com");
cookie.Domain = "test1.com";
cookie.Path = "/";
Response.Cookies.Add(cookie);


跨頂級(jí)域名
  如果我不是二級(jí)域名而是完全在不同頂級(jí)域名中,例如 www.test1.com 所在的web應(yīng)用程序創(chuàng)建了一個(gè)cookie,想要在 www.test2.com 或其二級(jí)域名的應(yīng)用程序中訪問,改怎么辦呢?我們知道靠常規(guī)反的方法是訪問不了的,關(guān)鍵我們就是看看有沒有方法可以訪問。事實(shí)是Cookie可以在一定條件下跨域,而不是隨心所欲的實(shí)現(xiàn)跨域。

  我們來做個(gè)測(cè)試,看看兩個(gè)站點(diǎn) www.test1.com 和 www.test2.com 如何實(shí)現(xiàn)cookie跨域訪問。 按照常規(guī)我們需要有2個(gè)頂級(jí)域名,并且有DNS服務(wù)器才能夠配置域名,否則我們是無法驗(yàn)證的,但是這里我們也沒有必要那么麻煩,我們可以通過修改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)地址,用兩個(gè)域名分別訪問就可以了。

  我們新建三個(gè)頁面,分別是 Default.aspx、SSO.ashx、GetCookie.aspx。

  其中Default.aspx是 www.test1.com 的頁面,訪問的地址是 http://www.test1.com/Default.aspx??匆幌虑芭_(tái)代碼,它沒有任何后臺(tái)代碼

復(fù)制代碼 代碼如下:

%@ 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>


另外一個(gè)是 SSO.ashx 頁面,我們認(rèn)為它是 www.test2.com 的頁面,前臺(tái)沒有任何代碼,后臺(tái)代碼如下:

復(fù)制代碼 代碼如下:

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下的頁面,沒有前臺(tái)代碼,只有后臺(tái)代碼:

復(fù)制代碼 代碼如下:

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)在我們?cè)L問測(cè)試,通過訪問 http://www.test1.com/Default.aspx 之后,這時(shí)會(huì)通過iframe載入調(diào)用SSO.ashx這個(gè)頁面,執(zhí)行后臺(tái)代碼創(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 的后臺(tái)代碼中有一句:context.Response.AddHeader("P3P", "CP=CAO PSA OUR"); 是用來設(shè)置P3P響應(yīng)頭。是因?yàn)镮E瀏覽器支持的P3P導(dǎo)致iframe跨站點(diǎn)時(shí)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中,這樣就簡(jiǎn)單的實(shí)現(xiàn)了cookie跨域的訪問。

  另外Default.aspx頁面也可改為JS調(diào)用形式:

復(fù)制代碼 代碼如下:

%@ 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>

您可能感興趣的文章:
  • asp.net下cookies的丟失和中文亂碼
  • asp.net清空Cookie的兩種方法
  • asp.net COOKIES需要注意的一點(diǎn)
  • asp.net cookie的操作,寫入、讀取與操作
  • asp.net Cookie跨域、虛擬目錄等設(shè)置方法
  • ASP.NET Cookie 操作實(shí)現(xiàn)
  • asp.net Cookie操作類
  • asp.net下cookies操作完美代碼
  • asp.net通過js實(shí)現(xiàn)Cookie創(chuàng)建以及清除Cookie數(shù)組的代碼
  • asp.net(C#)跨域及跨域?qū)慍ookie問題
  • asp.net中的cookie使用介紹
  • Asp.net內(nèi)置對(duì)象之Cookies(簡(jiǎn)介/屬性方法/基本操作及實(shí)例)
  • asp.net Cookie值中文亂碼問題解決方法
  • asp.net中使用cookie與md5加密實(shí)現(xiàn)記住密碼功能的實(shí)現(xiàn)代碼
  • ASP.NET之Response.Cookies.Remove 無法刪除COOKIE的原因
  • asp.net利用cookie保存用戶密碼實(shí)現(xiàn)自動(dòng)登錄的方法
  • ASP.NET中Cookie的使用方法

標(biāo)簽:棗莊 蚌埠 江蘇 廣元 萍鄉(xiāng) 衢州 衡水 大理

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《asp.net關(guān)于Cookie跨域(域名)的問題》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266