How do you configure SQL Session State in ASP.NET 5.0 (vNext) MVC 6?

1k views Asked by At

In Visual Studio 2015 RC, ASP.NET 5 vNext, i am trying to use SQL Session State in MVC 6. I'm not sure how to go about it. Can anyone show me a sample or give me some tips?

I'm just trying out Azure Load Balancer. I set up a load balanced set and linked two VM's to it. It's working as expected as I can see the machine name changing between the two VM's, when I go to a view that has the following code. @System.Environment.MachineName

I tried enabling session as shown in this link How to implement Session State in ASP.NET vNext MVC 6. but since this session is kept in the server, when the user is moved to a different server in the load balanced set, the session state does not persist between servers.

I know you can persist the same session if SQL Server Session State is used. In ASP.NET 4, it can be done easily just by changing sessionState element in web.config as follows.

<sessionState 
            mode="SQLServer"
            sqlConnectionString="data source=127.0.0.1;user id=<username>;password=<strongpassword>"
            cookieless="false" 
            timeout="20" 
    />

I'm hoping anyone can show me how this can be done in ASP.NET 5 (vnext)

1

There are 1 answers

2
Kiran On BEST ANSWER

Support for SQL Server Session State is currently in development. So your best options for now are using In-Memory session(which has the issue which you mentioned about state not persisted for multiple servers behind load balancer kind of scenarios). However you could try using Redis cache(ASP.NET 5 Session implementation is dependent on IDistributedCache based implementations of which there is an implementation for Redis cache).

public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<IDistributedCache, RedisCache>();
    services.AddSession();

//-----------

public void Configure(IApplicationBuilder app)
{
   app.UseSession();

You need to reference following packages:

Microsoft.Framework.Caching.Redis
Microsoft.AspNet.Session