HttpCookies Issue with Asp.Net Core 3.1 | Code2night.com
Code2night
  • Home
  • Blogs
  • Guest Posts
  • Tutorial
  • Post Blog
  • Register
  • Login
  1. Home
  2. Blogpost

HttpCookies Issue with Asp.Net Core 3.1

Date- Jun 07,2022

4263

AspNet core 31 Aspnet core 21

HttpCookies in Asp.Net Core 2.1

So, for a Asp.Net core 2.1 web application if you want to use HttpCookies you have to add this in your startup.cs file inside ConfigureServices method.

   services.Configure<CookiePolicyOptions>(options =>
          {
              options.CheckConsentNeeded = context => false;
              options.MinimumSameSitePolicy = Microsoft.AspNetCore.Http.SameSiteMode.None;
           });

After adding this, you can save your httpcookies like this

  CookieOptions option = new CookieOptions
            {
                Expires = DateTime.Now.AddDays(1)
            };
            Response.Cookies.Append("key", "value", option);

For getting the saved cookie value in your project you can do this

 var cookies = Request.Cookies["key"];

So, you can set your own keyname and use that to get your cookie value in Asp.net core 2.1. However you will notice if you try the same in Asp.Net core 3.1 , you will not be able to get your cookie value as values does not get saved used this method.

HttpCookie in Asp.net Core 3.1

So, for a Asp.Net core 2.1 web application if you want to use HttpCookies you have to add this in your startup.cs file inside ConfigureServices method.

 services.ConfigureApplicationCookie(options =>
            {
                // Cookie settings
                options.Cookie.HttpOnly = true;
                options.ExpireTimeSpan = TimeSpan.FromDays(1);

                options.SlidingExpiration = true;
            });

For saving the HttpCookie you can use the same method like Asp.net core 2.1 .

   CookieOptions option = new CookieOptions
            {
                Expires = DateTime.Now.AddDays(1)
            };
            Response.Cookies.Append("key", "value", option);

For getting the saved cookie value also we will use same method like we did for Asp.Net core 2.1

 var cookies = Request.Cookies["key"];

So this is how you can fix issue with HttpCookies when you upgrade your application from Asp.net core 2.1 to Asp.net Core 3.1.

Comments

Tags

LinkedinLogin
LinkedinProfile
GetLinkedinProfile
C#
Aspnet
MVC
Linkedin
ITextSharp
Export to Pdf
AspNet Core
AspNet
View to Pdf in Aspnet
Model Validation In ASPNET Core MVC 60
Model Validation
Model Validation In ASPNET Core MVC
Model Validation In ASPNET
Image Compression in AspNet
Compress Image in c#
AspNet MVC
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1210
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp Join Us On Facebook

Welcome To Code2night, A common place for sharing your programming knowledge,Blogs and Videos

  • Panipat
  • info@Code2night.com

Links

  • Home
  • Blogs
  • Tutorial
  • Post Blog

Popular Tags

Copyright © 2025 by Code2night. All Rights Reserved

  • Home
  • Blog
  • Login
  • SignUp
  • Contact
  • Terms & Conditions
  • Refund Policy
  • About Us
  • Privacy Policy
  • Json Beautifier
  • Guest Posts