Joe
Joe

Reputation: 81

using Redis Cache as a Session Storage asp.net core

Trying to use Redis Cache as a session store in an already existing Web App which is developed in asp.net mvc core ( 2.1.1) .

was referring https://garywoodfine.com/redis-inmemory-cache-asp-net-mvc-core/

and https://joonasw.net/view/redis-cache-session-store but when trying to check the session set/get values in Redis Desktop Manager nothing is shown.

Is there any additional steps required to make the session store to use the Redis Cache instead of the default in memory ( in-proc) option?

Startup.cs

    public void ConfigureServices(IServiceCollection services)
     {
      services.AddDistributedRedisCache(options =>
            {
                options.InstanceName = Configuration.GetValue<string> 
                          ("redis:name");
                options.Configuration = Configuration.GetValue<string> 
                                           ("redis:host");

              });

             services.AddMvc()
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
            .AddSessionStateTempDataProvider();
             services.AddSingleton<IDistributedCache, RedisCache>();

services.AddSession(options =>
            {
                options.IdleTimeout = TimeSpan.FromMinutes(60);
            });

 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }
            app.UseStaticFiles();
            app.UseCookiePolicy();
            app.UseSession();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Login}/{action=Login}/{id?}");
            });
        }
      }

appsettings

 "redis": {
    "host": "127.0.0.1",
    "port": 6379,
    "name": "localhost"
  },

Nuget package used

Microsoft.Extensions.Caching.Redis 2.1.1

Sample Usage in Action Method

 var storedValue = "Redis TimeStamp : " + DateTime.Now.ToString("s");
            HttpContext.Session.SetString("TestValue", storedValue);
            HttpContext.Session.CommitAsync();

Appreciate any pointers or direction on this.

TIA

Upvotes: 5

Views: 7573

Answers (1)

Gerardo
Gerardo

Reputation: 51

check this:

//Shared Session in RedisCache

using StackExchange.Redis;
using Microsoft.AspNetCore.DataProtection;

      public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

            services.AddDataProtection()
                .SetApplicationName("vextus")
                .PersistKeysToRedis(ConnectionMultiplexer.Connect(Configuration.GetConnectionString("RedisConnection")),
                                    "DataProtection-Keys");


            services.AddDistributedRedisCache(o =>
            {
                o.Configuration = Configuration.GetConnectionString("RedisConnection");
            });

            services.AddSession(o =>
            {
                o.Cookie.Name = "vextus";
                o.Cookie.SameSite = SameSiteMode.None;
                o.Cookie.HttpOnly = true;
                o.IdleTimeout = TimeSpan.FromMinutes(10);
            });
        }

Upvotes: 4

Related Questions