c# - 在 ASP.net Core 3.1 中设置会话超时
问题描述
我有一个网络应用程序 .net core 3.1 razor pages。我将会话超时设置为 30 分钟,但是它会在 10 分钟不活动后将当前用户注销。全局 IIS 设置在服务器上设置为 60 分钟。这是我在 Startup.cs 中的代码。我错过了什么?任何帮助是极大的赞赏。谢谢!
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddControllers().AddNewtonsoftJson(options =>
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
);
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddTransient<IMemberService, MemberService>();
services.AddTransient<INewsService, NewsService>();
services.AddTransient<IUserCardService, UserCardService>();
services.AddTransient<IUsernameService, UsernameService>();
services.AddTransient<ILinkService, LinkService>();
services.AddTransient<INotificationService, NotificationService>();
services.AddTransient<IEmailSender, EmailSender>();
services.Configure<AuthMessageSenderOptions>(Configuration);
services.AddDbContext<DefaultContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultContextConnection")));
services.AddControllersWithViews()
.AddSessionStateTempDataProvider();
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential
// cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
// requires using Microsoft.AspNetCore.Http;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddDistributedMemoryCache();
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(30);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
services.AddRazorPages()
.AddRazorRuntimeCompilation()
.AddSessionStateTempDataProvider();
services.AddSession();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
//app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseRewriter();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseSession();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
解决方案
推荐阅读
- c# - 我是否应该始终将 CancellationToken 添加到我的控制器操作中?
- c# - 查询字符串的属性路由
- python - 如何在 conftest.py 中访问 json 文件(像 config.json 这样的测试数据)
- javascript - JS:将所有 img src 相对 url 替换为绝对
- ms-access - 如果在访问 VBA 中选中复选框,则隐藏/取消隐藏按钮
- javascript - Vue.JS - 主要布局组件和路由
- powershell - 如何使用 DSC 在 Azure VM 上安装存储在 Azure 文件共享中的 exe 文件?
- c++ - C++ STL双向链表性能对比
- javascript - angularjs:检查数组r中是否存在id
- python - 从 Python 中的一行获取一个值,在多次迭代中将值保留在下一行进行处理。