Code2night
  • Home
  • Guest Posts
  • Tutorial
  • Languages
    • Angular
    • C
    • C#
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • SQL Server
    • TypeScript
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
    • Word Counter
    • Base64 Encode/Decode
    • Diff Checker
    • JSON to CSV
    • Password Generator
  • Register
  • Login
  1. Home
  2. Blogpost

HttpCookies Issue with Asp.Net Core 3.1

Date- Jun 07,2022

6243

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.

S
Shubham Batra
Programming author at Code2Night โ€” sharing tutorials on ASP.NET, C#, and more.
View all posts โ†’

Related Articles

Integrate Stripe Payment Gateway In ASP.NET Core 8.0
Nov 23, 2023
Integrate Stripe Payment Gateway In ASP.NET Core 7.0
Jul 22, 2023
Implement Stripe Payment Gateway In ASP.NET Core
Jul 01, 2023
Get Mime Type for any extension in Asp.Net
May 15, 2023

Comments

Tags

AspNet
C#
programming
AspNet MVC
c programming
AspNet Core
C
software development
tutorial
MVC
memory management
Paypal
coding
coding best practices
data structures
programming tutorial
tutorials
object oriented programming
Slick Slider
StripeNet
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 | 1760
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
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blogs
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
Free Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Diff Checker
  • Base64 Encode/Decode
  • Word Counter
By Language
  • Angular
  • C
  • C#
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ยท  Terms
Translate Page