无法从同一域Asp.Net上的不同Web应用程序中的其他页面读取cookie

时间:2013-04-17 18:43:36

标签: asp.net web-applications cookies

当我从同一个域上的另一个Web应用程序打开一个页面时,我正在尝试读取在原始页面上创建的cookie,但是我无法读取cookie。如果我将该页面移动到同一个Web应用程序然后它可以工作,但那不是我想要的。

[Domain: MyCompany.mobi]
[WebApp A] - create cookie and launch page

Protected Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
  Dim aCookie As New HttpCookie("TestCookie")
  aCookie.Value = "Hello World"
  aCookie.Expires = DateTime.Now.AddDays(1)
  Response.Cookies.Add(aCookie)

  Dim script = "window.open('http://MyCompany.mobi/webappB/default.aspx?id=TestCookie')"

  'open page in WebsiteB
  ScriptManager.RegisterStartupScript(Me, Me.GetType, "OpenPage", script, True)
End Sub

[Domain: MyCompany.mobi]
[WebApp B] - read the cookie and display in label

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim id As String = ""

    If Request.QueryString("id") IsNot Nothing Then id = Request.QueryString("id").ToString

    If Request.Cookies(id) IsNot Nothing Then
        Dim aCookie As HttpCookie = Request.Cookies(id)
        Label1.Text = aCookie.Name & " : " & aCookie.Value
    Else
        Label1.Text = "Cannot read cookie"
    End If

End Sub

1 个答案:

答案 0 :(得分:0)

Cookie与域名相关联;而不是DNS指向的物理服务器。

出于安全原因,您无法从其他域名读取Cookie 您需要传递URL中的信息。