Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading cookies (on the server) written by a different host on the same domain

Tags:

asp.net-mvc

In one ASP.NET MVC app on a domain "mysite.com", I'm writing a cookie to a specific domain, say ".mysite.com". I'm able to confirm that the browser accepts my cookie.

Then, from another ASP.NET MVC app, say "jmp.mysite.com", I'm trying to read the cookie set by the first app.

The problem? Well, I can't read the cookie. My browser says it's there, but my web server says it isn't.

Is there some special way of reading these sorts of cookies? Could IIS perhaps not be sending them to ASP.NET?

like image 283
JMP Avatar asked Feb 24 '23 12:02

JMP


1 Answers

To create the cookie from foo.mysite.com:

public ActionResult Index()
{
    var cookie = new HttpCookie("foo", "bar")
    {
        HttpOnly = true,
        Domain = "mysite.com"
    };
    return View();
}

and to read the cookie from jmp.mysite.com:

public ActionResult Index()
{
    var cookie = Request.Cookies["foo"];
    if (cookie != null)
    {
        var value = cookie.Value;
        // TODO: do something with the value
    }

    return View();
}
like image 57
Darin Dimitrov Avatar answered Feb 27 '23 06:02

Darin Dimitrov