【问题标题】:Accessing a cookie created on one subdomain on another subdomain访问在另一个子域的一个子域上创建的 cookie
【发布时间】:2013-10-14 17:15:40
【问题描述】:

给定:

Domain 1: subdomain1.mydomain.com
Domain 2: subdomain2.mydomain.com

我使用下面的代码在“域 1”上创建了一个 cookie,并尝试访问“域 2”上的 cookie。

我的问题是“域 2”不想识别 cookie。是什么赋予了?我认为问题出在 .Domain 属性上,但我把句号放在前面,所以我错过了什么?

public void CreateCookie()
{
    Boolean bNew = false;

    HttpCookie oCookie = HttpContext.Current.Request.Cookies.Get("myData");
    if (null == oCookie)
    {
        oCookie = new HttpCookie("myData");
        bNew = true;
    }

    // Set the cookie value.
    oCookie.Domain = ".mydomain.com";
    oCookie.Secure = false;
    oCookie["myid"] = "myid@whatever";
    oCookie.Expires = DateTime.Now.AddDays(7);

    if (true == bNew)
        HttpContext.Current.Response.Cookies.Add(oCookie);
    else
        HttpContext.Current.Response.Cookies.Set(oCookie);
}

public String GetCookie()
{
    String myid = null;

        HttpCookie oCookie = HttpContext.Current.Request.Cookies.Get("myData");
        if (null != oCookie)
        myid = HttpContext.Current.Server.HtmlEncode(oCookie["myid"]);

    return myid;
}

想法?

【问题讨论】:

    标签: c# .net cookies httpcookie


    【解决方案1】:

    我做了更多研究,并在另一个 stackoverflow.com 票证上找到了答案,请参阅here

    基本上,代码变化是:

    oCookie.Domain = "mydomain.com";
    oCookie.Path = "/";
    
    1. 域名前没有句点。
    2. 添加值为“/”的路径属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-04
      • 1970-01-01
      • 2023-03-29
      • 2011-01-11
      • 2015-01-05
      • 1970-01-01
      • 1970-01-01
      • 2019-05-18
      相关资源
      最近更新 更多