asp.net MVC 页面在几天后重定向到登录页面



我有一个 ASP.NET 的MVC页面,每5分钟刷新一次。此页面仅用于显示数据。用于托管此页面的 PC 几天没有任何交互。该页面将在几天内正常刷新,但随后(自行(此页面将在 2、4 或 5 天后随机重定向到登录页面。

知道发生了什么吗?

我只是将以下属性添加到此页面的 Web.config 中,因为我认为在数据库在夜间运行备份或类似奇怪的事情时可能超时。

<location path="~/Views/Production/WIPScanStationViewer.aspx">
<system.web>
<httpRuntime executionTimeout="1000" maxRequestLength="2048576"/>      
</system.web>    
</location>

您的会话 cookie 似乎即将过期。尝试打开该页面,按 F12 打开开发工具,然后转到"应用程序"选项卡。在那里,展开"Cookie"节点并搜索您的会话 Cookie。"过期/最长期限"列应显示到期日期,以便您能够知道这是否是问题背后的原因。

如果这是问题所在,并且加载该页面的Web浏览器永远不会关闭,则可以将值更改为"会话",这意味着它没有到期日期,取而代之的是,一旦Web浏览器关闭,cookie就会过期。

您也可以将 cookie 设置为在 X 天甚至几个月后过期,但老实说,我不太喜欢生命周期很长的 cookie,特别是因为如果 cookie 在任何时候遭到入侵,只要 cookie 不过期,攻击者就可以访问您的会话。

您是否使用任何形式的身份验证/授权?如果是这样,重定向到登录屏幕应该是这里的线索。您的 Cookie 可能设置为在 X 天后过期。如果使用 ASPNet 标识检查Startup.cs>>Startup.ConfigureAuth(IAppBuilder app)

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))
}
});

由于您使用的是表单身份验证,因此检查此超时值的正确位置是您的 Web 配置。只要您的cookie是自动生成的,您应该在配置中看到类似的东西

<!--
forms Attributes: 
name="[cookie name]" - Sets the name of the cookie used for Forms 
Authentication.
loginUrl="[url]" - Sets the URL to redirect client to for authentication.
protection="[All|None|Encryption|Validation]" - Sets the protection mode for 
data in cookie.
timeout="[minutes]" - Sets the duration of time for cookie to be valid 
(reset on each request).
path="/" - Sets the path for the cookie.
requireSSL="[true|false]" - Should the forms authentication cookie be sent 
only over SSL?
slidingExpiration="[true|false]" - Should the forms authentication cookie 
and ticket be reissued if they are about to expire?
--> 

如果您的 cookie 是手动生成的,则只需更新FormsAuthenticationTicket类即可获得正确的超时值。另请注意,如果您的 .config 文件中有 cookie 设置值,但在代码中的某个地方手动生成票证/cookie,则手动生成将覆盖配置值。

请参阅此 msdn 类规范以设置代码中的超时:https://msdn.microsoft.com/en-us/library/system.web.security.formsauthenticationticket.aspx

此外,如果 cookie 不是问题所在,您可能还需要在 web.config 中检查您的会话状态。看看这个家伙在这里..非常简单:https://msdn.microsoft.com/en-us/library/h6bb9cz9(v=vs.71(.aspx

最新更新