JMP
JMP

Reputation: 7834

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

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?

Upvotes: 4

Views: 7030

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038850

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();
}

Upvotes: 8

Related Questions