DomBurf
DomBurf

Reputation: 2522

Add Session storage to ASP.NET Core 3.0

We are currently migrating an existing ASP.NET Core 2.2 web application to 3.0 So far we've got most things working, except session storage.

We had this fully working in v2.2 as we used it to hold the current logged in user's details. Now that we've upgraded to v3.0 it no longer works.

Here's the middleware code.

public void ConfigureServices(IServiceCollection services)
{

    // configure Razor pages, MVC, authentication here

    services.AddDistributedMemoryCache();

     services.AddSession(options =>
     {
        //prevent session storage from being accessed from client script 
        //i.e. only server side code (added security) 
        options.Cookie.HttpOnly = true;
    });
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseSession();
}

N.B. I've removed the rest of the middleware code for clarity.

I've tried moving the app.SetSession() line to the top of the method in case the order of execution was the problem but this has made no difference.

When I hover over the HttpContent.Session property in the debugger I get the following error:

HttpContext.Session threw an exception of type System.InvalidOperationException

How do I enable Session storage in ASP.NET Core 3.0?

Upvotes: 0

Views: 1068

Answers (1)

DomBurf
DomBurf

Reputation: 2522

I've just tried adding the app.UseSession() to the top of the method and it's working now. It definitely didn't work before but it's working now and that's the main thing.

Upvotes: 1

Related Questions