【问题标题】:using Redis Cache as a Session Storage asp.net core使用 Redis 缓存作为会话存储 asp.net 核心
【发布时间】:2019-05-13 13:33:32
【问题描述】:

尝试将 Redis 缓存用作在 asp.net mvc 核心 (2.1.1) 中开发的现有 Web 应用程序中的会话存储。

指的是https://garywoodfine.com/redis-inmemory-cache-asp-net-mvc-core/

https://joonasw.net/view/redis-cache-session-store 但是当尝试在 Redis 桌面管理器中检查会话设置/获取值时,没有显示任何内容。

是否需要任何其他步骤才能使会话存储使用 Redis 缓存而不是默认的内存 (in-proc) 选项?

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?}");
            });
        }
      }

应用设置

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

使用了 Nuget 包

Microsoft.Extensions.Caching.Redis 2.1.1

操作方法中的示例用法

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

感谢任何关于此的指针或方向。

TIA

【问题讨论】:

  • 一般来说,我会推荐 StackExchange.Redis 来满足您的 .net redis 需求。我很确定 Azure Redis 团队会推荐相同的方法,如果您想在 github 上四处游荡。
  • @NoRefundsNoReturns 感谢您的建议.. 即使我也在考虑同样的问题,但以前没有使用过。

标签: c# session caching redis asp.net-core-mvc


【解决方案1】:

检查一下:

//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);
            });
        }

【讨论】:

    猜你喜欢
    • 2020-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-22
    • 2014-09-28
    相关资源
    最近更新 更多