Andrew
Andrew

Reputation: 2939

Redis - Moving from IIS session state

I am trying to understand how to implement redis by replacing all IIS session state values with the redis cache. I have redis working using a docker image. I am using a C# .Net Forms web app. I have included the StackExchange.Redis nuget package and set this up so far.

public class Redis
{
    private static readonly Lazy<ConnectionMultiplexer> LazyConnection = new Lazy<ConnectionMultiplexer>(() =>
    {

        var redisConnectionString = ConfigurationManager.AppSettings["RedisConnectionString"];
        var options = ConfigurationOptions.Parse(redisConnectionString);
        options.AbortOnConnectFail = false;

        return ConnectionMultiplexer.Connect(options);
    });

    public static ConnectionMultiplexer Connection => LazyConnection.Value;
}

But I am confused how to store the user context session key value pairs. By this I mean when I store a key for the user, say "UserId" can the key just be "UserId"? or do I needs to prefix it with a unique user specific context id. I cant find anything on how redis would work based on different users. How does it know the context of the user and hence how to get the correct key?

All I'm reading is that its a hashtable that stores values, which is fine for a single UserId, but I'm going to have lots of users with a UserId?

If anyone can help me understand this, that would be great, thanks you

Upvotes: 4

Views: 6439

Answers (1)

Andrew
Andrew

Reputation: 2939

Ok so after looking at the following link I was able to get it all working

https://learn.microsoft.com/en-us/azure/azure-cache-for-redis/cache-aspnet-session-state-provider

  1. I installed a redis docker image as follows on the command line, replacing your your_long_password_here, you can also just specify port 6379:6379, but I wanted to see what happens if I used another port. The password is important as otherwise it will run in protected mode which means only localhost(loopback) calls can be made, which helps protect it more, as pre version 3.2 this was not the case!
docker run -p 8055:6379 --name redis --restart=always -d redis –-requirepass <your_long_password_here>
  1. The run this command to make sure it was running, you should see that the port has the values you provided
docker container ls
  1. Then in your .net project install the following nuget packagefrom the nuget package manager console or use the GUI package manager. You will need to use .net 4.6.2 for the current version.
Install-Package Microsoft.Web.RedisSessionStateProvider
  1. Once installed comment out or remove this line in your Web.config
<sessionState mode="InProc" timeout="60" />
  1. Then update or change the new provider that the nuget package will have added to your Web.config. This should be inside the system.web tag. Note that I used a connection string as this will not be hosted on azure as of yet, but there are lots of confusing extra options to configure it.
<sessionState mode="Custom" customProvider="MySessionStateStore">
  <providers>
  <add name="MySessionStateStore"
       type="Microsoft.Web.Redis.RedisSessionStateProvider"
       connectionString="localhost:8055,password=your_long_password_here"/>
  </providers>
</sessionState>
  1. You will need to make sure all the objects that you were storing in the iis session have the [Serializable] attribute as otherwise it wont work and will throw an error.

You should now see that you can run your website seamlessly, and you will now be using the redis cache and no longer the session state.

Upvotes: 7

Related Questions