Sharing Session between ASP.NET Forms and ASP.NET Core

2k Views Asked by At

I have an existing ASP.NET Forms web application that uses RedisSessionProvider. This is setup via just the WebConfig with the following.

<sessionState mode="Custom" customProvider="RedisSessionProvider">
  <providers>
    <add name="RedisSessionProvider" applicationName="myApp" type="Microsoft.Web.Redis.RedisSessionStateProvider" connectionString="RedisConnection" ssl="true" />
  </providers>
</sessionState>
<connectionStrings>
<add name="RedisConnection" connectionString="host:port,password=myPassword,ssl=True,abortConnect=False"/></connectionStrings>

I would like to share this with an ASP.NET Core SubSite/SubApplication. I'm able to share it fine with other subsites that are ASP.NET (MVC/Forms) by just adding the reference and adding the above to their WebConfigs.

However, for .NET Core, I can't figure out how to share it. Keep in mind that the code for the ASP.NET forms site is on a freeze for 3 months, so we can't make any changes to it.

In the ASP.NET Core application i've referenced Microsoft.AspNetCore.DataProtection and StackExchange.Redis. And have the following code in Startup.cs

public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddSingleton<IConfiguration>(Configuration);
        string redisConnectionString = Configuration.GetValue<string>("Redis:ConnectionString");
        string redisKey = Configuration.GetValue<string>("Redis:Key");
        var redis = ConnectionMultiplexer.Connect(redisConnectionString);
        services.AddDataProtection().PersistKeysToRedis(redis, redisKey).SetApplicationName("myApp");
        services.AddAuthentication("Identity.Application").AddCookie("Identity.Application", 
            options =>
        {
            options.Cookie.Name = "ASP.NET_SessionId";
        });
        services.AddSession(options =>
        {
            options.Cookie.Name = "ASP.NET_SessionId";
        });
        services.AddOptions();
    }

And i've added app.UseSession() to public void Configure(...).

This does rename the cookie to the same as the host site but it doesn't give me access to the same Session Store. I'm guessing this has something to do with the way the cookies are encrypted in .NET core vs ASP.NET.

Is what i'm trying to do possible without modifying the Host .NET Forms site? If it's only possible if i modified the host (would have to be in the future), what would i have to do there?

0

There are 0 best solutions below