好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

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

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

熱門標簽:太原外呼電銷機器人費用 電話機器人廣告話術 朝陽市地圖標注 外呼系統用員工身份證 使用智能電話機器人違法嗎 保山電話外呼管理系統怎么用 蘇州銷售外呼系統預算 淘寶地圖標注如何做 東莞語音電銷機器人排名

跨二級域名
  我們知道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.com
127.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>

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

標簽:綏化 克拉瑪依 運城 呼倫貝爾 洛陽 阿里 西藏 潛江

巨人網絡通訊聲明:本文標題《asp.net關于Cookie跨域(域名)的問題》,本文關鍵詞  asp.net,關于,Cookie,跨域,域名,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《asp.net關于Cookie跨域(域名)的問題》相關的同類信息!
  • 本頁收集關于asp.net關于Cookie跨域(域名)的問題的相關信息資訊供網民參考!
  • 推薦文章