C# 如何防止用户使用“后退”按钮成功登录后返回登录页面
我正在开发一个MVC3应用程序,遇到了登录安全问题。该场景是当用户使用用户名和密码登录时,如果正确,他/她将被重定向到其主页C# 如何防止用户使用“后退”按钮成功登录后返回登录页面,c#,asp.net,asp.net-mvc,login,C#,Asp.net,Asp.net Mvc,Login,我正在开发一个MVC3应用程序,遇到了登录安全问题。该场景是当用户使用用户名和密码登录时,如果正确,他/她将被重定向到其主页 但是,如果他们点击浏览器返回按钮,他们会返回到登录页面,在我的情况下,我不想这样做。这与facebook、gmail等类似。一旦用户使用其凭据登录,他们就无法通过单击浏览器的“后退”按钮返回登录页面。您可以使用javascript检查成功登录后提供的cookie。js将在页面加载时检查它,如果cookie存在,则重定向到非登录页面。还有其他方法,如中所述: 您需要使缓存和
但是,如果他们点击浏览器返回按钮,他们会返回到登录页面,在我的情况下,我不想这样做。这与facebook、gmail等类似。一旦用户使用其凭据登录,他们就无法通过单击浏览器的“后退”按钮返回登录页面。您可以使用javascript检查成功登录后提供的cookie。js将在页面加载时检查它,如果cookie存在,则重定向到非登录页面。还有其他方法,如中所述:
您需要使缓存和头过期,以下是我使用的:
<% HttpContext.Current.Response.Cache.SetAllowResponseInBrowserHistory(false);
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
HttpContext.Current.Response.Cache.SetValidUntilExpires(false);
HttpContext.Current.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
HttpContext.Current.Response.Cache.SetNoStore();
Response.Cache.SetExpires(DateTime.Now);
System.Web.HttpContext.Current.Response.AddHeader("Pragma", "no-cache");
Response.Cache.SetValidUntilExpires(true);
Response.Buffer = true;
Response.ExpiresAbsolute = DateTime.Now.Subtract(new TimeSpan(1, 0, 0, 0));
Response.Expires = 0;
Response.CacheControl = "no-cache";
Response.Cache.SetExpires(DateTime.UtcNow.AddYears(-4));
Response.ExpiresAbsolute = DateTime.Now.Subtract(new TimeSpan(1, 0, 0, 0));
Response.AppendHeader("Pragma", "no-cache");
Response.Cache.AppendCacheExtension("must-revalidate, proxy-revalidate, post-check=0, pre-check=0");
%>
<script language="javascript" type="text/javascript">
window.onbeforeunload = function () {
// This function does nothing. It won't spawn a confirmation dialog
// But it will ensure that the page is not cached by the browser.
}
</script>
window.onbeforeunload=函数(){
//此函数不执行任何操作。它不会生成确认对话框
//但它将确保浏览器不会缓存页面。
}
将此添加到页眉中,下次用户尝试返回时,它将请求新页面加载。还要注意,您应该使用Server.Transfer而不是Response.Redirect on login submit