Access Cookies in Controller that are set in javascript Asp.Net Core

681 Views Asked by At

I'm setting a cookie in javascript but it's not available when I try to access it in my Controller.

javascipt:

createCookie('wh', 'hello', 1);

export function createCookie(name: string, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toUTCString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

controller action:

public IActionResult TestCookie()
{
    var cookies = HttpContext.Request.Cookies;
    string result = "";

    foreach (var item in cookies)
    {
        result += item.Key + ": " + item.Value + ", ";
    }

    ViewData["result"] = result;

    return View();
}

If I set a cookie in the controller, then it's available on subsequent requests, the Auth cookies are there, but no "wh" cookie. What am I doing wrong?

0

There are 0 best solutions below