Home > Enterprise >  Session sharing between Asp.Net Webforms and Asp.Net Core
Session sharing between Asp.Net Webforms and Asp.Net Core

Time:09-10

I have an old ASP.Net Webforms project that we are transitioning over to a .NET 6.0 Core project. The product owners want to be able to use both projects in conjunction until the final .NET 6.0 project is completed. There are items in the ASP.Net Webforms project stored in session state that I need to be able to access from the Core project and visa versa. How can I share session state between these 2 applications?

CodePudding user response:

If you can make some minor changes to the web forms app, there are some packages in preview that help with exactly this scenario. The System.Web adapter packages are intended to make it easier to run ASP.NET and ASP.NET Core apps side-by-side while slowly moving functionality from the ASP.NET app over to the ASP.NET Core one.

The feature that applies here is shared session.

Basically, you'll need to add a reference to the Microsoft.AspNetCore.SystemWebAdapters.SessionState package in both apps (soon to be renamed in the next preview to Microsoft.AspNetCore.SystemWebAdapters.CoreServices and Microsoft.AspNetCore.SystemWebAdapters.FrameworkServices).

Once that's done, you'll add a module to your ASP.NET app so that it is able to securely make session information available to other apps (the feature works by having the ASP.NET Core app defer to the ASP.NET app to read or write session state). The code will go in your Application_Start method and look like this:

SystemWebAdapterConfiguration.AddSystemWebAdapters(this)
    .AddProxySupport(options => options.UseForwardedHeaders = true)
    .AddRemoteApp(options =>
    {
        options.ApiKey = "<A strong, unique key>";
    })
    .AddRemoteAppSession()
    .AddJsonSessionSerializer(options => 
    {
        // Register session item names/types that will be read or written
        options.KnownKeys.Add("test-value", typeof(int));
        options.KnownKeys.Add("SampleSessionItem", typeof(SessionDemoModel));
    });

Also, add the module to the web.config:

<modules>
  <add name="SystemWebAdapterModule" type="Microsoft.AspNetCore.SystemWebAdapters.SystemWebAdapterModule, Microsoft.AspNetCore.SystemWebAdapters" preCondition="managedHandler" />
</modules>

In the ASP.NET Core app, you'll make similar changes to setup session services and point them at the ASP.NET app.

While registering services in startup:

builder.Services.AddSystemWebAdapters()
    .AddRemoteApp(options =>
    {
        options.RemoteAppUrl = new("<ASP.NET app's URL>");
        options.ApiKey = "<A strong, unique key>";
    })
    .AddRemoteAppSession()
    .AddJsonSessionSerializer(options =>     {
    {
        // Register session item names/types that will be read or written
        options.KnownKeys.Add("test-value", typeof(int));
        options.KnownKeys.Add("SampleSessionItem", typeof(SessionDemoModel));
    });

Also, add the System.Web adapters middleware in your middleware pipeline:

app.UseSystemWebAdapters();

As a perf optimization, remote session state is only available in controllers/action methods that are annotated with the [Session] attribute, so apply that, as necessary, or apply it globally using the RequireSystemWebAdapterSession extension method when mapping routes.

Once that's done, the ASP.NET Core app can access session using the same APIs as the ASP.NET app would have:

// Read
var model = System.Web.HttpContext.Current?.Session?["SampleSessionItem"] as SessionDemoModel;

// Write
System.Web.HttpContext.Current.Session["SampleSessionItem"] = demoModel;

There are some complete samples in the System.Web adapters GitHub repo, as well.

  • Related