Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Session has not been configured for this application or request error

I am very new to asp.net I recently I came across this exception:

System.InvalidOperationException

The details of the exception says:

Session has not been configured for this application or request.

Here is the code snippet where it happens:

[HttpPost]         public object Post([FromBody]loginCredentials value)         {             if (value.username.Equals("Admin")                 &&                 value.password.Equals("admin"))             {                 HttpContext.Session.Set("userLogin", System.Text.UTF8Encoding.UTF8.GetBytes(value.username)); //This the line that throws the exception.                 return new                 {                     account = new                     {                         email = value.username                     }                 };             }             throw new UnauthorizedAccessException("invalid credentials");         } 

I have no idea why it's happening or what does this error actually mean. Can someone please explain what might be causing this?

like image 562
Keselme Avatar asked May 31 '18 08:05

Keselme


People also ask

How do you solve session has not been configured for this application or request?

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.

How does .NET Core determine environment?

To determine the runtime environment, ASP.NET Core reads from the following environment variables: DOTNET_ENVIRONMENT. ASPNETCORE_ENVIRONMENT when ConfigureWebHostDefaults is called. The default ASP.NET Core web app templates call ConfigureWebHostDefaults .


2 Answers

In your Startup.cs you might need to call

app.UseSession before app.UseMvc

app.UseSession();    app.UseMvc();  

For this to work, you will also need to make sure the Microsoft.AspNetCore.Session nuget package is installed.

Update

You dont not need to use app.UseMvc(); in .NET Core 3.0 or higher

like image 123
Austin Born Avatar answered Sep 29 '22 03:09

Austin Born


Following code worked out for me:

Configure Services :

    public void ConfigureServices(IServiceCollection services)     {           //In-Memory           services.AddDistributedMemoryCache();           services.AddSession(options => {           options.IdleTimeout = TimeSpan.FromMinutes(1);           });                         // Add framework services.           services.AddMvc();      } 

Configure the HTTP Request Pipeline:

public void Configure(IApplicationBuilder app, IHostingEnvironment env,                        ILoggerFactory loggerFactory) {     loggerFactory.AddConsole(Configuration.GetSection("Logging"));     loggerFactory.AddDebug();     if (env.IsDevelopment())     {        app.UseDeveloperExceptionPage();        app.UseBrowserLink();     }     else     {        app.UseExceptionHandler("/Home/Error");     }     app.UseStaticFiles();     app.UseSession();     app.UseMvc(routes =>     {         routes.MapRoute(            name: "default",            template: "{controller=Home}/{action=Index}/{id?}");      });  } 
like image 29
Techiemanu Avatar answered Sep 29 '22 04:09

Techiemanu