5

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

2
  • 1
    Generally I would recommend StackExchange.Redis for your .net redis needs. I'm pretty sure the Azure Redis team recommends the same if you want grovel around on github. Commented Dec 12, 2018 at 16:58
  • @NoRefundsNoReturns thanks for the suggestion .. even I was thinking about the same but was not having used it before. Commented Dec 13, 2018 at 5:45

1 Answer 1

4

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);
            });
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.