【问题标题】:Distributed cache Session returns NULL on seperate requests, why?分布式缓存会话在单独的请求中返回 NULL,为什么?
【发布时间】:2020-06-10 07:42:06
【问题描述】:

请帮忙! 该项目是一个 .NetCore 3.1 MVC Web 应用程序。 我在尝试使我的 Session 与我的分布式缓存嵌入一起工作时遇到问题。 缓存运行良好,它保存到 SQL 服务器并且检索也很好。 然后我测试了会话......如果我在一个请求上设置,并尝试从另一个请求中获取,我总是得到 NULL,我已经查看了所有可能的指南并且无法解决问题。 值得注意的是会话正在保存到我的数据库中,我只是无法访问它! 这是我的创业公司:

#define SQLServer // Redis





using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

using Microsoft.AspNetCore.Builder;

using Microsoft.AspNetCore.Hosting;

using Microsoft.AspNetCore.Http;

using Microsoft.Extensions.Configuration;

using Microsoft.Extensions.DependencyInjection;

using Microsoft.Extensions.Hosting;



namespace DigitalBusiness.Caching.WebTester

{

    publicclassStartup

    {

        publicStartup(IConfiguration configuration, IWebHostEnvironment hostEnviroment)

        {

            Configuration = configuration;

            HostEnviroment = hostEnviroment;

        }



        public IConfiguration Configuration { get; }

        public IWebHostEnvironment HostEnviroment { get; }



        // This method gets called by the runtime. Use this method to add services to the container.

        publicvoid ConfigureServices(IServiceCollection services)

        {

            services.AddControllersWithViews();

            services.AddHttpContextAccessor();

            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();



            if (HostEnviroment.IsDevelopment())

            {

                services.AddDistributedMemoryCache();

                services.AddSession();

            }

            else

            {

#if SQLServer

                services.AddDistributedSqlServerCache(options => {

                    options.ConnectionString = Configuration["RawData"];

                    options.SchemaName = "dbo";

                    options.TableName = "SessionCache_Test_To_Remove";

                });

                services.AddSession(opts => {

                    opts.IdleTimeout = TimeSpan.FromSeconds(60);

                    opts.Cookie.HttpOnly = true;

                    opts.Cookie.IsEssential = true;

                });



#else

                services.AddStackExchangeRedisCache(options => {

                    options.Configuration = "localhost";

                    options.InstanceName = "SampleInstance"

                });

#endif

            }



        }



        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

        publicvoid Configure(IApplicationBuilder app, IWebHostEnvironment env)

        {

            if (env.IsDevelopment())

            {

                app.UseDeveloperExceptionPage();

            }

            else

            {

                app.UseExceptionHandler("/Home/Error");

            }

            app.UseStaticFiles();



            app.UseRouting();



            app.UseAuthorization();



            app.UseSession();



            app.UseEndpoints(endpoints =>

            {

                endpoints.MapControllerRoute(

                    name: "default",

                    pattern: "{controller=CacheTester}/{action=Index}/{id?}");

            });

        }

    }

}

这是我的控制器方法。 注意:当我尝试访问会话数据时,我看到第一次调用中的 sessionCookie 被返回给客户端。但它没有被添加到对服务器的后续请求中......此外,Expire 始终为 -1。




        public void GetSession()

        {
//When i call this method, after i called the "SetSession" before, i get NULL

            var t = HttpContext.Session.GetString("123");

        }


        [HttpPost]

        public void SetSession([FromBody]model postData)

        {

            HttpContext.Session.SetString("123", "asd");

//Here i get the session value with no issues!

            var t = HttpContext.Session.GetString("123");


        }

请救救我:(提前谢谢你们!

【问题讨论】:

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


    【解决方案1】:

    我花了将近两天的时间才发现在一个意想不到的地方产生的问题...... 该死的客户!我使用了 MDN 的 fetch,但没有添加“Allow-Credentials: true”,这意味着 cookie 从未被浏览器保存...

    一个简单的新手错误,最终花费了这么多时间...... 我希望如果您碰巧遇到同样的问题,您将能够使用我的答案更快地解决它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-27
      • 2017-04-04
      • 2017-11-25
      • 2016-06-10
      • 2014-02-02
      • 1970-01-01
      • 2013-03-05
      相关资源
      最近更新 更多