dotnet / systemweb-adapters

MIT License
335 stars 61 forks source link

Sharing Session between .NET Framework application and .Net7 application is not working #453

Open meghanavedpathak opened 9 months ago

meghanavedpathak commented 9 months ago

I am trying to Set Session variable in .NET framework application and access it in .NET 7 application using link https://learn.microsoft.com/en-us/aspnet/core/migration/inc/remote-session?view=aspnetcore-7.0 however I am unable to get value of session variable in .NET Core.

I did following changes

.NET Framework - Added Nuget package Microsoft.AspNetCore.SystemWebAdapters.FrameworkServices

Added below code in global.asax.cs -Application_Start function

SystemWebAdapterConfiguration.AddSystemWebAdapters(this) .AddJsonSessionSerializer(options => { // Serialization/deserialization requires each session key to be registered to a type options.RegisterKey("test");
}) // Provide a strong API key that will be used to authenticate the request on the remote app for querying the session // ApiKey is a string representing a GUID .AddRemoteAppServer(options => options.ApiKey = "03f74e3e-d690-4806-b0a7-e66f89215bb3") .AddSessionServer();

.NET 7 Changes

builder.Services.AddSystemWebAdapters() .AddJsonSessionSerializer(options => { // Serialization/deserialization requires each session key to be registered to a type options.RegisterKey("test");
}) .AddRemoteAppClient(options => { // Provide the URL for the remote app that has enabled session querying options.RemoteAppUrl = new(builder.Configuration["ReverseProxy:Clusters:fallbackCluster:Destinations:fallbackApp:Address"]); // Provide a strong API key that will be used to authenticate the request on the remote app for querying the session options.ApiKey = "03f74e3e-d690-4806-b0a7-e66f89215bb3"; }) .AddSessionClient();

Added app.MapDefaultControllerRoute().RequireSystemWebAdapterSession(); ;

sgarnovsky commented 9 months ago

I have got something the same but under .Net 8. Originally I'd already created a project using .Net 6 and SystemWebAdapters using version 1.2.0 and it worked fine. After migrating to the .Net 8 and SystemWebAdapters 1.3.0 (nuget package) Session from the System.Web.HttpContext is returned as a null reference.

One thing I see this github project refers 1.2.0 version. It is possible the 1.3.0 has changed initialization steps or just moved to other place.

So for now, I just downgraded the SystemWebAdapters packages back to 1.2.0 version. Confirmed the Session sharing works fine under .Net 8 in this case.

lalitkhanna commented 9 months ago

@sgarnovsky I've tried with all versions .Net6,7,8. Its not at all working, Can you pls share your code ?

sgarnovsky commented 9 months ago

My code is here. It's mostly the same as yours above. I only use a custom session serializer. Followed instructions from this video to make it working. https://www.youtube.com/watch?v=zXoGXbB3JDI

services.AddSystemWebAdapters() .AddSessionSerializer() .AddMssAdminSessionSerializer() .AddRemoteAppClient(options => { options.RemoteAppUrl = new(Configuration.GetMigrationLegacyAppAddress()); options.ApiKey = Configuration.GetMigrationRemoteAPIKey(); }) .AddSessionClient();

app.MapRazorPages() .RequireSystemWebAdapterSession();

lalitkhanna commented 9 months ago

@sgarnovsky I did same but somehow still System.Web.HttpContext.Current.Session is null. Any Possibilities if you can share your code in Github? I'm using .Net 8 & SystemWebAdapters ver is 1.3 & also tried 1.2 but nothing happened.

sgarnovsky commented 9 months ago

Unfortunately, I can't share my code (it is a big customer project). I guess, as a session is null even for the 1.2 package version, you may miss to call the RequireSystemWebAdapterSession in a right way... Please try a [Session] attribute too, as described here: https://stackoverflow.com/questions/73638526/session-sharing-between-asp-net-webforms-and-asp-net-core

Also, do not forget to apply a middleware initialization call: app.UseSystemWebAdapters();

And of course, check the session instance from the "System.Web.HttpContext": System.Web.HttpContext.Current?.Session

lalitkhanna commented 9 months ago

Dear @sgarnovsky

Able to Achieve , now its working. Thankyou so much for your help.

sgarnovsky commented 9 months ago

@lalitkhanna please post the details you found to fix on your side. And, of course, let us know what a version of SystemWebAdapters you ended up to use as well as .Net version.

lalitkhanna commented 9 months ago

@sgarnovsky I found couple of points for my fix.

  1. Add all four .Netget Packages for Microsoft.AspNetCore.SystemWebAdapters.FrameworkServices
  2. Added [Session] attribute in Controller
  3. Microsoft.AspNetCore.SystemWebAdapters.FrameworkServices 1.2 Ver
  4. .Net 8

Rest all points are same which @meghanavedpathak added.

sgarnovsky commented 9 months ago

@lalitkhanna thank you for details. Please let us know if you tried the 1.3 version.

meghanavedpathak commented 9 months ago

I tried by using .Net 6 and SystemWebAdapters using version 1.2.0 but its not working on my side. We do not want application to be on .NET 8 currently. I will check video you provided. Thank you for help.

Jbarker1 commented 8 months ago

Same here, using .net 8 and going back to version 1.2.0 with the rest following the remoteauth identity sample works.

lalitkhanna commented 8 months ago

Another issue, while trying to access the Asp.net web form pages then getting ERR_HTTP2_PROTOCOL_ERROR .(in console)

Anybody face this issue ?