c# - 防止注销后查看页面

标签 c# asp.net forms-authentication back-button browser-history

这是谷歌中一个非常著名的问题。我找到了几个实现此功能的建议。我所实现的程序描述如下:

我在主页中添加了注销链接,单击该链接后,我会将用户重定向到注销页面。

protected void LinkButton1_Click(object sender, EventArgs e) {      
    Response.Redirect("../Logout.aspx");
}

现在我在 Logout.aspx 中添加了:

Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
Response.Cache.SetNoStore();
Response.AppendHeader("Pragma", "no-cache");

在Page_Load方法代码后面。

此外,我还向该 Logout.aspx 添加了 asp:ScriptManagerasp:Timer:

<asp:ScriptManager ID="ScriptManager1" runat="server"> 
</asp:ScriptManager> 
<asp:Timer ID="Timer1" runat="server" Interval="1000" ontick="Timer1_Tick" > 
</asp:Timer>

Timer1_Tick方法是:

protected void Timer1_Tick(object sender, EventArgs e) {
    FormsAuthentication.SignOut();
    Session.Abandon();
    FormsAuthentication.RedirectToLoginPage();
}

这是从 Logout.aspx 重定向到 Login.aspx。我还在 Logout.aspx 中添加了以下 JavaScript 方法:

function disableBackButton() {
    window.history.forward(1);
}
disableBackButton();
window.onload = disableBackButton();
window.onpageshow = function (evt) { if (evt.persisted) disableBackButton() }
window.onunload = function () { void (0) } 

只有当我单击“后退”按钮,或单击多次并暂停时,它才起作用。但是,如果连续多次单击它,那么我将再次被放置在主页中。

如何解决这个问题?

最佳答案

我使用以下注销,清除cookie,并且我在“真正”注销我的用户时没有遇到任何问题。

编辑:

请注意,浏览器通常会在其历史记录中缓存页面,并且我认为您无法阻止它们在注销后显示页面!

FormsAuthentication.SignOut();
Session.Abandon();

// clear authentication cookie
HttpCookie cookie1 = new HttpCookie(FormsAuthentication.FormsCookieName, "");
cookie1.Expires = DateTime.Now.AddYears(-1);
Response.Cookies.Add(cookie1);

// clear session cookie
HttpCookie cookie2 = new HttpCookie("ASP.NET_SessionId", "");
cookie2.Expires = DateTime.Now.AddYears(-1);
Response.Cookies.Add(cookie2);

FormsAuthentication.RedirectToLoginPage();

关于c# - 防止注销后查看页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9965919/

相关文章:

c# - dll 中的静态属性生存期或缓存方法

c# - 错误 :System. Text.Encoding' 不包含 'Default' 的定义

c# - 不用VS编译C#项目

asp.net - 使连接字符串的 AttachDbFilename 在配置文件中相对

c# - 如何在 ASP.Net MVC 5 中跟踪匿名用户的事件?

tomcat - 使用 cURL 的 j_security 身份验证

asp.net-mvc - Simplemembership ASPXAUTH cookie 在两个单独的 Web 项目上验证

c# - Unity如何改变 Canvas 的渲染顺序

c# - ASP.NET MVC 错误 : The attempted operation is not supported for the type of object referenced

asp.net-mvc - MVC2 应用程序(和其他应用程序)共享 WCF 服务和身份验证