HttpCookies Issue with Asp.Net Core 3.1
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.