How to share session between framework 4.8 and .net with YARP

Royden 20 Reputation points
2025-02-12T10:19:32.33+00:00

I realise there is plenty, plenty, of documentation/videos/help but nothing is helping me get past the very basic act of sharing session even though I see it work in the "UpgradeSample-main" example project.

I'm finding resources are often different, or out of date and nothing appears to work for me.

I've done a side-by-side comparison, I've restarted my attempts many times, I am not winning and would appreciate some help with this please.

Thank you

Developer technologies | .NET | Other
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 25,296 Reputation points
    2025-02-14T10:00:51.44+00:00

    @Royden, based on my test, I reproduced the problem that I always get null Session Value. I did some search; it may be due to the version changing. I recommend that you could use Cookie to replace Session, which is more coinvent.

    Please try the following Changes.

    Asp.net MVC (.NET Framework):

    Place the ViewBag.UserName in index.html

    <section class="row" aria-labelledby="aspnetTitle">
        <h1 id="title">ASP.NET</h1>  
        <p>My Name is @ViewBag.UserName</p>
    
    
    

    Controller:

    public class HomeController : 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();
         }
         
     }
    
    
    

    ASP.NET Core MVC (.NET):

    Place the ViewBag.UserName in index.html

    <div class="text-center">
        <h1 class="display-4">Welcome</h1>
        <p>Learn about <a href="https://learn.microsoft.com/aspnet/core">building Web apps with ASP.NET Core  </a>.</p>
        <p>My Name is @ViewBag.UserName</p>
    </div>
    
    
    

    Controller:

        public class HomeController : Controller
        {
            private readonly ILogger<HomeController> _logger;
            public HomeController(ILogger<HomeController> logger)
            {
                _logger = logger;
            }
            public IActionResult Index()
            {
                var sharedSession = Request.Cookies["SharedSession"];
                if (sharedSession != null)
                {
                    // Use the session value
                    var sessionValue = sharedSession;
                    ViewBag.UserName = sessionValue;
                }
                return View();
            }
       
        }
    
    
    

    Please run your asp.net mvc(.NET Framework) first, then run your asp.net core mvc, you will see the following tested result:

    User's image

    Hope it could help you.

    Best Regards,

    Jack

    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.


1 additional answer

Sort by: Most helpful
  1. Jack J Jun 25,296 Reputation points
    2025-02-12T11:54:34.6566667+00:00

    @Royden, Welcome to Microsoft Q&A, if you want to share session data between .net framework app and .NET app with YARP, I recommend that you could create a Session database to share session data between .NET Framework 4.8 app and .NET app. For .NET Framework 4.8:

    <sessionState mode="SQLServer"
                  sqlConnectionString="Data Source=ServerName;Initial Catalog=SessionDB;Integrated Security=True"
                  cookieless="UseCookies" timeout="30" />
    

    For .NET : Install Microsoft.Extensions.Caching.SqlServer and configure session state:

    services.AddDistributedSqlServerCache(options =>
    {
        options.ConnectionString = "Data Source=ServerName;Initial Catalog=SessionDB;Integrated Security=True";
        options.SchemaName = "dbo";
        options.TableName = "SessionData";
    });
    

    Hope it could help you.


    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.


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.