gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 04:11:11 25 4
gpt4 key购买 nike

我正在尝试将 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com