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?
-
I would create a new main project with the state data which launches the new and old projects passing an instance of the state to both apps. – jdweng Sep 07 '22 at 16:15
-
@jdweng - But how do you "pass an instance of the state"? That's the part I need help with. Also, it should be noted that the Webforms application cannot be modified or changed. – Icemanind Sep 07 '22 at 16:18
-
You need to make minor modifications to startup of Webform for testing. – jdweng Sep 07 '22 at 16:51
1 Answers
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.

- 381
- 1
- 5
-
1This looks like a perfect solution for me, but I must be doing something wrong. For some reason, in the Core application, the Session is always `null`. Any ideas? – Icemanind Sep 08 '22 at 03:30
-
1The `System.Web.HttpContext.Session` property should be set by middleware that is added by the UseSystemWebAdapters call any time the endpoint has [Session] metadata. So, a couple things to check: 1. Is your Core app calling `app.UseSystemWebAdatpers()` to register the System.Web adapters middleware (which includes session stuff)? 2. Is your controller or action method where you're trying to use session decorated with a `[Session]` attribute (or, alternatively, have you called `RequireSystemWebAdapterSession()` after `app.MapDefaultControllerRoute()` to enable session everywhere)? – MJRousos Sep 13 '22 at 18:46