2013-07-28 96 views
4

我正在研究MVC3应用程序,并且遇到登录安全问题。这种情况是当用户使用他/她的用户名和密码登录时,如果正确,他/她将被重定向到他们的主页。如何防止用户在使用后退按钮成功登录后返回登录页面

但是,如果他们点击浏览器后退按钮,他们会回到登录页面,在我的情况下,我不想要。它与facebook,gmail等相同,用户一旦用他/她的凭证登录,就不能通过单击浏览器的后退按钮返回到登录页面。

回答

6

您可以使用JavaScript来检查您在成功登录后提供的cookie。如果cookie存在,那么js将检查它是否在页面加载并重定向到非登录页面。也有其他方法来做到这一点的desctibed在: here

+1

谢谢你的答案。它为我工作。 –

+0

@CodeKaro你能把它标记为正确答案吗? – yossico

+0

谢谢@CodeKaro – yossico

1

你需要到期高速缓存和头,这里是我使用:

<% 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> 

在页面头部添加这个,下一次用户尝试回去它会请求新的页面加载。

+0

还要注意,你应该使用Server.Transfer而不是Response.Redirect登录提交 – Alok

相关问题