如何在 .NET Core 5 中增加会话超时时间

huangapple go评论71阅读模式
英文:

How to increase session timeout in .net core 5

问题

以下是您要翻译的内容:

"我已经尝试延长我的 .net core 5.0 应用程序中的会话超时时间,但似乎应用程序具有默认或覆盖方法,将会话超时时间设置为小于30分钟。

以下是我尝试配置会话超时的地方。

services.ConfigureApplicationCookie(options =>
{
// Cookie 设置
options.Cookie.HttpOnly = true;
options.ExpireTimeSpan = TimeSpan.FromHours(48);
options.LoginPath = "/Account/Login";
options.LogoutPath = "/Account/Logout";
options.AccessDeniedPath = new PathString("/Administration/AccessDenied");
options.SlidingExpiration = true;
});

services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromHours(48);
options.Cookie.IsEssential = true;
});

我做错了什么?请帮助。"

英文:

I have tried to extend the session timeout in my .net core 5.0 application but it seems the application has a default or an overriding method that enables session timeout to less than 30 minutes.

The below are where I have tried configuring the session timeout.

 services.ConfigureApplicationCookie(options =>
        {
            // Cookie settings
            options.Cookie.HttpOnly = true;
            options.ExpireTimeSpan = TimeSpan.FromHours(48);
            options.LoginPath = "/Account/Login";
            options.LogoutPath = "/Account/Logout";
            options.AccessDeniedPath = new PathString("/Administration/AccessDenied");
            options.SlidingExpiration = true;
        });

 services.AddSession(options =>
        {
            options.IdleTimeout = TimeSpan.FromHours(48);
            options.Cookie.IsEssential = true;

        });

What is it am I doing wrong.
Kindly assist.

答案1

得分: 1

我测试了你的显示代码,应该是有效的,以下是测试结果:如何在 .NET Core 5 中增加会话超时时间 你可以从图片中看到,会话的过期时间是有效的。

所以也许你可以尝试以下步骤来检查是什么导致了你的问题:

  1. 确保你在 Startup.cs 中添加了 app.UseSession();
  2. 检查你的代码中是否有其他地方设置了较短的会话超时时间。例如,如果你有一个中间件设置了更短的会话超时时间,它可能会覆盖你的配置。
  3. 检查会话中间件是否是在身份验证中间件之后添加的。身份验证中间件设置了自己的超时时间,如果会话中间件在其之前添加,它将使用那个值。

或者你可以分享更多关于你的项目的信息,我们可以检查可能导致问题的原因。

英文:

I test your showing codes which should be working, here is the test result:如何在 .NET Core 5 中增加会话超时时间 You can see from the picture, the expire time of session is working

So maybe you can try these steps to check what cause your issue:

  1. Make sure you are adding the app.UseSession(); in the Startup.cs
  2. Check that there are no other places in your code where the session timeout is being set as a shorter time. For example, if you have a middleware that sets the lower session timeout, it could be overriding your configuration.
  3. Check that the session middleware is being added after the authentication middleware. The authentication middleware sets its own timeout, and if the session middleware is added before it, it will use that value instead.

Or maybe you can share more information of your project and we can check what may cause your issue.

huangapple
  • 本文由 发表于 2023年2月16日 19:46:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75471807.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定