skip to Main Content

I used the following code to set a cookie in ASP.NET Core 6, but no cookie is created with this code in my browser.

var options = new CookieOptions()
        {
            Expires = DateTime.Now.AddDays(2),
            Path = "/"
        };

 Response.Cookies.Append(CookieName, serializer.Serialize(cartItems), options);

Please, if anyone knows what the problem is or if there has been a change in .NET 6, I would appreciate it if they could help

2

Answers


  1. Chosen as BEST ANSWER

    I had used this piece of code in my program class.

    services.Configure<CookiePolicyOptions>(options =>
            {
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.Lax;
            });
    

    and in pipeline

    app.UseCookiePolicy();
    

    But when I removed these codes, cookies were added


  2. alright, i tested this in asp.net 6 and its working for me. can you try this format?

    var cookieOptions = new CookieOptions(); 
    cookieOptions.Expires = DateTime.Now.AddDays(1);
    cookieOptions.Path = "/";
    Response.Cookies.Append("SomeCookie", "SomeValue", cookieOptions);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search