Sharing ASP.NET cookies across sub-domains

前端 未结 4 1958
野趣味
野趣味 2020-12-01 03:47

I have two sites, both on the same domain, but with different sub-domains.
site1.mydomain.com site2.mydomain.com

Once I\'m authenticated on each, I look at the c

4条回答
  •  萌比男神i
    2020-12-01 04:18

    set the property of Domain to ".mydomain.com" in each Cookies of two subdomains websites

    like

    Response.Cookies["test"].Value = "some value";
    Response.Cookies["test"].Domain = ".mysite.com";
    

    UPDATE 1

    in Site

    HttpCookie hc = new HttpCookie("strName", "value");
    hc.Domain = ".mydomain.com"; // must start with "."
    hc.Expires = DateTime.Now.AddMonths(3);
    HttpContext.Current.Response.Cookies.Add(hc);
    

    In Site B

    HttpContext.Current.Request.Cookies["strName"].Value
    

    Try It

    Regards

提交回复
热议问题