简体   繁体   English

访问在另一个子域上的一个子域上创建的cookie

[英]Accessing a cookie created on one subdomain on another subdomain

Given: 鉴于:

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

I create a cookie on "Domain 1" using the code below and trying to access the cookie on "Domain 2". 我使用下面的代码在“域1”上创建一个cookie,并尝试访问“域2”上的cookie。

My problem is that "Domain 2" does not want to recognize the cookie. 我的问题是“域2”不想识别cookie。 What gives? 是什么赋予了? I figure the problem is with the .Domain property, but I put the period before, so what am I missing? 我认为问题在于.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;
}

Thoughts? 思考?

I did some more research and I found the answer on another stackoverflow.com ticket, see here . 我做了一些更多的研究,我在另一个stackoverflow.com票上找到了答案,请看这里

Basically, the code changes are: 基本上,代码更改是:

oCookie.Domain = "mydomain.com";
oCookie.Path = "/";
  1. No period before the domain name. 域名之前没有期限。
  2. Add in a path property with the value of "/". 添加值为“/”的路径属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM