Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Session variable value is getting null in ASP.NET Core

I am setting a session variable in one method and trying to get the session variable value from the another method in a controller but its always getting null:

Here is my code:

public class HomeController : Controller {     public IActionResult Index()     {          HttpContext.Session.SetString("Test", "Hello!");         var message = HttpContext.Session.GetString("Test");// Here value is getting correctly         return View();     }      public IActionResult About()     {         var message = HttpContext.Session.GetString("Test"); // This value is always getting null here          return View();     } } 

Here is my session configuration in Startup class:

In ConfigureServices() method:

services.Configure<CookiePolicyOptions>(options => {     // This lambda determines whether user consent for non-essential cookies is needed for a given request.     options.CheckConsentNeeded = context => true;     options.MinimumSameSitePolicy = SameSiteMode.None; });  services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); services.AddDistributedMemoryCache(); services.AddMvc().AddSessionStateTempDataProvider(); services.AddSession(options => {     options.Cookie.Name = "TanvirArjel.Session";     options.IdleTimeout = TimeSpan.FromDays(1); }); 

In Configure() method:

app.UseSession(); app.UseMvc(routes => {     routes.MapRoute(         name: "default",         template: "{controller=Home}/{action=Index}/{id?}"); }); 

Very strange and peculiar problem! Any help will be highly appreciated!

like image 624
TanvirArjel Avatar asked Apr 11 '18 08:04

TanvirArjel


People also ask

Why HttpContext current session is null?

Session will never be null if you have any variable in the Session. HttpContext. Current. Session represents a collection of all the session values that you are storing in the session.

Can we use session in .NET core?

Session state. Session state is an ASP.NET Core scenario for storage of user data while the user browses a web app. Session state uses a store maintained by the app to persist data across requests from a client. The session data is backed by a cache and considered ephemeral data.

How can we enable session in asp net core?

Session can be enabled using the Configure method. Inside this method, you will have to call the UseSession method of the app object. Note: It is mandatory to call the UseSession method before the UseMvc method. //Enable Session.


1 Answers

For ASP.NET Core 2.1 and 2.2

In the ConfigureServices method of the Startup class, Set options.CheckConsentNeeded = context => false; as follows:

services.Configure<CookiePolicyOptions>(options => {   // This lambda determines whether user consent for non-essential cookies is needed for a given request.   options.CheckConsentNeeded = context => false;   options.MinimumSameSitePolicy = SameSiteMode.None; }); 

Problem solved!

like image 163
TanvirArjel Avatar answered Sep 19 '22 19:09

TanvirArjel