【发布时间】:2019-02-11 18:54:19
【问题描述】:
我有一个使用 MVC 5 构建的应用程序,我们有一个场景,当默认会话超时已经过去时,即使用户仍在文本框上打字,用户也会被重定向到登录页面。我希望仅当页面空闲超过 15 分钟时才将用户重定向到登录页面,而不是在用户非常空闲时。在 web 表单中,我们使用在配置文件中将滑动过期设置为 true,但这在 MVC 中不起作用。如有任何帮助,我将不胜感激。
这是我尝试过的:
[HttpPost]
[AllowAnonymous]
public ActionResult Login(LoginViewModel model, string returnUrl, string
command)
{
string decodedUrl = "";
if (!string.IsNullOrEmpty(returnUrl))
decodedUrl = Server.UrlDecode(returnUrl);
FormsAuthentication.SetAuthCookie(model.Email, false);
var authTicket = new FormsAuthenticationTicket(1, user.Email,
DateTime.Now, DateTime.Now.AddMinutes(15), false, user.Roles);
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName,
encryptedTicket);
HttpContext.Response.Cookies.Add(authCookie);
authCookie.Expires = authTicket.Expiration;
if (Url.IsLocalUrl(decodedUrl))
{
return Redirect(decodedUrl);
}
else
{
return RedirectToAction("analytics", "dashboard");
}
}
全球 ASAX 代码:
protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
{
try
{
var authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie != null)
{
FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
if (authTicket != null && !authTicket.Expired)
{
var roles = authTicket.UserData.Split(',');
HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(new FormsIdentity(authTicket), roles);
}
}
}
catch(Exception ex)
{
}
}
【问题讨论】:
标签: c# asp.net-mvc