David Merrilees David Merrilees - 1 month ago 4
ASP.NET (C#) Question

Does modifying the Response.Cookies collection also change Request.Cookies in .Net?

Just want to check what i'm seeing, modifying the Response.Cookies collection also changes Request.Cookies?

Answer

Yes, it does. Try creating a new page with the following in the codebehind:

protected void Page_Load(object sender, EventArgs e)
{
    if (Request.Cookies["newCookie"] == null)
    {
        Response.Cookies["newCookie"].Value = "Hello, world!";
        var myValue = Request.Cookies["newCookie"].Value;
    }
}

Clear your cookies and place a breakpoint on the closing brace of the condition. You'll see that myValue has the value "Hello, world!" on the first request.