Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at

  • Billy 53 posts 244 karma points c-trib
    Feb 01, 2022 @ 11:15

    Create cookie in surface controller

    Hi, I'm trying to create a cookie in a surface controller with following code:

    public IActionResult SaveCookiePreferences(CookiesConsentViewModel model)
            if (!ModelState.IsValid)
                return CurrentUmbracoPage();
            var cookieOptions = new CookieOptions()
                Path = "/",
                HttpOnly = false,
                IsEssential = true,
                Expires = DateTime.Now.AddYears(2)
            Response.Cookies.Append("cookiename", "cookievalue", cookieOptions);
            return CurrentUmbracoPage();

    When the page reloads, and I read my cookie it is not set. But when I reload the page again. The cookie exists and has the correct value.

    What could be the issue here?

    In our v8 .NET code I remember setting both Request and Response cookies like this:

    HttpContext.Request.Cookies[CookiesConsentCookieKey][ExplicitConsentCookieKey] = bool.TrueString;
    HttpContext.Response.Cookies[CookiesConsentCookieKey][ExplicitConsentCookieKey] = bool.TrueString;

    But .NET core only allows setting Response cookies.

    Thanks for any advice!

  • Damian 65 posts 388 karma points
    Feb 06, 2022 @ 20:37

    Howdy friend,

    You're definitely looking to set a Request cookie, this much i know. -I'm still dipping the toes into dotnet-core myself; i haven't had the need to do this task yet so can't share direct experience but the following article appears to be on-point for setting the Request cookie:


    This is also something new to me, not likely related, but a good to know to file away in the brain for later days should it (inevitably?) become relevant:

  • Billy 53 posts 244 karma points c-trib
    Feb 07, 2022 @ 07:29

    The cookie setting code was not the problem.

    I updated

    return CurrentUmbracoPage();


    return RedirectToCurrentUmbracoPage();

    This fixed the issue.

Please Sign in or register to post replies

Write your reply to: