gpt4 book ai didi

asp.net-mvc - session 超时在ASP.NET MVC中不起作用

转载 作者:行者123 更新时间:2023-12-02 08:22:01 25 4
gpt4 key购买 nike

我尝试使用al.web.config和UseCookieAuthentication()方法上的设置,如下面列出的Web上许多主题所示:

Session timeout does not work ( is set in web.config )

How to set session timeout in web.config

mvc 5 session timeout after default period (20 mins)

但是,尝试将这些配置或方法中所有选项的 session 超时更改为1分钟(以进行测试)没有任何意义,而且我不确定错误在哪里。这是我更改的以下配置。有解决问题的主意吗?我还需要弄清楚在MVC应用程序中设置 session 超时的最好的主意是什么:web.config中的 或Auth类中的

web.config:

<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" executionTimeout="60" />

<sessionState mode="InProc" timeout="1" />

<!-- For LDAP -->
<httpCookies httpOnlyCookies="true" />
<authentication mode="Forms">

<!-- Note: I also remove this part and try with only "sessionState" -->
<forms name=".ADAuthCookie" loginUrl="~/Account/Login" timeout="1"
slidingExpiration="false" protection="All" />
</authentication>
</system.web>

Startup.Auth.cs:
public void ConfigureAuth(IAppBuilder app)
{
// Code removed for brevity.

// Configure the sign in cookie
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
// Enables the application to validate the security stamp when the user logs in.
// This is a security feature which is used when you change a password or add an external login to your account.
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(1),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(1));
app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);
}

最佳答案

如果使用ASP.NET Identity,则无需使用web.config中的设置。只需将这两行添加到UseCookieAuthentication()方法中,如下所示:

....,
SlidingExpiration = true,
ExpireTimeSpan = TimeSpan.FromMinutes(1)
...

因此,方法的最终代码如下所示:

Startup.Auth.cs:
public void ConfigureAuth(IAppBuilder app)
{
// Code removed for brevity.

// Configure the sign in cookie
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
// Enables the application to validate the security stamp when the user logs in.
// This is a security feature which is used when you change a password or add an external login to your account.
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
},
SlidingExpiration = true,
ExpireTimeSpan = TimeSpan.FromMinutes(1) //Set the session timeout at here
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(1));
app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);
}

有关更多信息,请访问 ASP.NET-Identity-Cookie-Authentication-Timeouts。希望这可以帮助...

关于asp.net-mvc - session 超时在ASP.NET MVC中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36196439/

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