How to share "System.Web.HttpContext.Current.Session" between ASP.NET and .NET CORE application

Marján Zoltán 20 Reputation points
2024-12-18T15:28:55.29+00:00

Dear All,

I would like to share session values between ASP.NET and ASP.NET CORE application. It is working fine only when the session value is set inside "Session_Start() method in Global.asax.cs. When I try to set session value in an other action in an other controller of the ASP.NET application, the session value is NULL in the .NET CORE application.

What am I doing wrong?

program.cs:

// Creating listener:

builder.Services

*.AddSystemWebAdapters()*

*.AddJsonSessionSerializer(options =>*

*{*

    *options.RegisterKey<string>("testvalueString");*

*})*

*.AddSessionSerializer(options =>*

*{*

   *options.ThrowOnUnknownSessionKey = true;*

*})*

*.AddRemoteAppClient(options =>*

*{*

    *options.RemoteAppUrl = new Uri("http://localhost:65020/teszt/ind/");//http://localhost:65020/teszt/ind/*

    *options.ApiKey = "4ffb49b3-2aa3-40b3-96ca-1b5331aab2dc";*

*})*

*.AddSessionClient();*

(...)

// Creating server:

Global.asax.cs, Application_Start() action:

(...)

SystemWebAdapterConfiguration

*.AddSystemWebAdapters(this)*

*.AddJsonSessionSerializer(options =>*

*{*

    *options.RegisterKey<string>("testvalueString");*

    *options.RegisterKey<DateTime>("tesvalueDateTime");*

*})*

*.AddSessionSerializer(options =>*

*{*

    *options.ThrowOnUnknownSessionKey = true;*

*})*

*.AddRemoteAppServer(options =>*

*{        options.ApiKey = "4ffb49b3-2aa3-40b3-96ca-1b5331aab2dc";// "<A strong, unique key>";*      

*})*

*.AddSessionServer();*

(...)

Settins session value

System.Web.HttpContext.Current.Session["testvalueString"] = "Test value";

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,706 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,550 questions
0 comments No comments
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 32,736 Reputation points Microsoft Vendor
    2024-12-19T07:49:37.5033333+00:00

    Hi @Marján Zoltán

    Please check the SystemWebAdapters package version and your project version. From this issue, it seems that if using SystemWebAdapters 1.3.0 and .Net 8, System.Web.HttpContext.Session might be null. You can check your application and package version and try to use the 1.2 version on .NET 6.

    I also tried to use the SystemWebAdapters package to share the session, but all failed, not matter setting session in Session_Start or controller. I suggest you can also post this issue on SystemWebAdapters Forum, and let the SystemWebAdapters team member give some suggestion.

    To share session between ASP.NET and .NET CORE application, there have a workaround to share session value via cookie, you can refer to it:

    Code in ASP.NET Controller:

            public ActionResult Index()
            {
                var sessionValue = "JohnDoe";
                // Store a value in the session
                Session["UserName"] = "JohnDoe";
                // In a controller or action in the MVC app
                var cookie = new HttpCookie("SharedSession", sessionValue)
                {
                    HttpOnly = true, // Secure it to prevent client-side JavaScript access
                    Expires = DateTime.Now.AddMinutes(30), // Set expiration for the session
                    Domain = "localhost" // Set to the shared domain for subdomains
                };
                Response.Cookies.Add(cookie);
                ViewBag.UserName = sessionValue;
                return View();
            }
    

    In ASP.NET Core Application:

            public IActionResult Index()
            {
                var sharedSession = Request.Cookies["SharedSession"];
                if (sharedSession != null)
                {
                    // Use the session value
                    var sessionValue = sharedSession;
                    ViewBag.UserName = sessionValue;
                } 
                return View();
            }
    

    Note: In this workaround, the cookies should in the same domain. Refer to this article: Share authentication cookies among ASP.NET apps

    Besides, you can also database or Redis to share data between ASP.NET and .NET CORE application.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Dillion

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. SurferOnWww 3,616 Reputation points
    2024-12-18T22:15:51.79+00:00

    It is not possible.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.