session - AddDistributedSqlServerCache 没有将 session 保存到数据库中

标签 session asp.net-core

我正在尝试将 HttpContext.Session 保存到数据库而不是内存服务器中

问题是什么都没有保存在表中, session 总是为空。

这是我的启动代码:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddDistributedSqlServerCache(options =>
        {
            options.ConnectionString = @"Data Source=localhost;Initial Catalog=mydb;Integrated Security=True;";
            options.SchemaName = "dbo";
            options.TableName = "SQLSessions";
        });

        services.AddSession(options => {
            options.CookieName = "Test.Session";
            options.IdleTimeout = TimeSpan.FromMinutes(60);
        });

    }


   public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IDistributedCache cache)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
            app.UseBrowserLink();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseStaticFiles();

        app.UseSession();

        app.UseForwardedHeaders(new ForwardedHeadersOptions
        {
            ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
        });
        app.UseHttpMethodOverride();
        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}"
                );
            routes.MapRoute(
                name: "AdvertisementImages",
                template: "{controller=Home}/{action=Images}/{type}/{name}", // URL with parameters
                defaults: new {type = 0, name = ""} // Parameter defaults
            );
        });

    }

和我用于保存 session 的 Controller 代码:

HttpContext.Session.SetObjectAsJson("TryToLogin", newLogin);

和我的 session 扩展:

   public static class SessionExtensions
    {
        public static void SetObjectAsJson(this ISession session, string key, object value)
        {
            session.SetString(key, JsonConvert.SerializeObject(value));
        }

        public static T GetObjectFromJson<T>(this ISession session, string key)
        {
            var value = session.GetString(key);

            return value == null ? default(T) : JsonConvert.DeserializeObject<T>(value);
        }
    }

这是我的 session 表架构:

enter image description here

最佳答案

关于session - AddDistributedSqlServerCache 没有将 session 保存到数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43198332/

相关文章:

zend-framework - Zend session : Common session at multi apps

php - session_regenerate_id 没有创建新的 session ID

java - Spring MVC 的 Cookie 问题

asp.net - Mac OS X Docker基准ASP.NET错误:找不到libdl

teamcity - 是否可以将 TeamCity 配置为使用 Visual Studio 2015 附带的 MSBuild?

python - 如何由于 Django 中的不活动而使 session 过期?

session - SailsJs 10 rc 9 connect-redis 错误

asp.net-core - 本地主机使用不受支持的协议(protocol)

asp.net - 如何使 Asp.NET Core MVC 应用程序在授权重定向上使用外部 url?

c# - 不等待所有任务是否安全