关闭浏览器时,自动从ASP.NET中的表单身份validation注销

当浏览器关闭或用户输入新地址时,有没有办法强制ASP.NET从其身份validation中退出?

如果浏览器保持打开状态,那么我需要对用户进行身份validation,因此最好在身份validation票证上超时。

不确定这是否仍然是一个问题,但这解决了我的问题。 只需将以下内容添加到起始页的Page_Load事件中:

protected void Page_Load(object sender, EventArgs e) { if (Request.UrlReferrer == null || string.IsNullOrEmpty(Request.UrlReferrer.AbsolutePath)) { Session.Abandon(); FormsAuthentication.SignOut(); FormsAuthentication.RedirectToLoginPage(); } } 

我已经研究了一段时间,阅读并看到各种post和答案,猜想和推测。

这个解决方案确实有一个重要的警告 – 弹出窗口(“BURN HIM !!!”我听到你哭)是必需的,JavaScript也是如此。 但是,鉴于您需要如此安全地实现这一点,我猜测用户会接受这一点以保持安全性,并且您可以编写启用JavaScript的代码。

步骤1 – validation是否已启用弹出窗口 – 如果未启用,请重定向到有关如何启用的说明

在Global.asax中

设置会话变量以validation是否已检查弹出窗口:

 void Session_Start(object sender, EventArgs e) { // Code that runs when a new session is started Session["popupsChecked"] = false; } 

在Page.aspx / masterPage.master中

检查会话变量,如果为false(首次访问此会话的站点),请注入JavaScript以检查弹出可用性:

 if (!IsPostBack) { if (!(bool)Session["popupsChecked"]) { Page.Header.Controls.Add(new LiteralControl("")); } } 

popupCheck.js文件 (文件“enablePopups.aspx”是关于如何为相关网站启用弹出窗口的说明)

 $(function () { result = window.open("/static/popupCheck.aspx", "popped", "width=10, height=10, location=no, menubar=no, status=no, toolbar=no, scrollbars=no, resizable=no"); if (result != null) { // Not blocking if (window.location.pathname != "/static/enablePopups.aspx") { window.location = "/"; }; } else { //blocking if (window.location.pathname != "/static/enablePopups.aspx") { window.location = "/static/enablePopups.aspx"; }; } }); 

最后, popup.Check.aspx

  Popup Checker    
Popup windows are enabled, please close this window.

在代码隐藏中使用以下代码 – 这将停止任何进一步的检查以查看是否启用了弹出窗口:

 protected void Page_Load(object sender, EventArgs e) { Session["popupsChecked"] = true; } 

所以在这一点上我们现在“强迫”用户为这个网站启用弹出窗口 – 如上所述,网站内容应该certificate我认为这种烦恼是合理的

第2步 – 检查它们的去向,如果它们关闭,则弹出注销

在您的主要javascript块,文件,数据中,或者您现在正在使用它

 var siteLinkClicked = false; var isAuthenticated = false; $(function() { // Check if user is authenticated if ($("#someElementThatOnlyAppearsWhenLoggedIn").length) { isAuthenticated = true; }; // Check if any of the links clicked are in the site $("a, input").click(function () { // -- You may need more then "a" and "input" tags, or exceptions siteLinkClicked = true; }); $(window).bind("beforeunload", function (event) { if (siteLinkClicked == false && isAuthenticated == true) { // popup the logout page window.open("/popupLogout.aspx", "popupLogout", "status=0,location=0,directories=0,menubar=0,scrollbar=0,resizable=0,width=400,height=200") }; }); }); 

还有popupLogout.aspx

我们有一些javascript来检查父(开启者)位置,如果它与此弹出窗口相同(即用户点击刷新 – 或者你错过了一个元素用于siteLinkClicked )然后只是关闭窗口而不做任何事情:

 $(function () { setTimeout(checkParent, 5000); // Give some time for the new window to load if they've navigated away - A counter could be added, logging off in 5... 4... 3... You get the idea. window.blur(); // And stick this to the back }); function checkParent() { var openerLocation = null; try { openerLocation = window.opener.location.hostname } catch (e) { openerLocation = null; } if (openerLocation == window.location.hostname) { window.close(); } else { $("#<%= cmdLogout.ClientID %>").click(); // setTimeout(window.close(), 1000); // Removed and add a redirect to static "loggedOut.html page }; }; 

如果位置不同/未定义,则触发按钮单击,然后关闭窗口:

然后该按钮会触发以下代码:

 protected void cmdLogout_click(object sender, EventArgs e) { System.Web.Security.FormsAuthentication.SignOut(); Session.Abandon(); Response.Redirect("~/static/loggedOut.htm"); // Added, can self close if needed in the HTML } 

最后的限制重申

没有JavaScript或弹出窗口,这种方法将无法工作,至少对我来说,我将在一个安全至关重要的受控环境中工作,因此这个解决方案值得最终用户的烦恼。

如果你必须走到这些极端,那么我只能假设数据和应用程序足够敏感,迫使用户必须启用javascript并且必须启用弹出窗口。

您似乎可以设置较长的到期时间,并将其设置为不持续以获得您似乎正在寻找的确切行为。 例如:

 FormsAuthentication.SetAuthCookie("username", true); 

这会将其设置为持久存储,以便当您关闭浏览器时,它将保留cookie,这样即使他们关闭浏览器并再次加载您的站点,也会使它们只要到期就不必登录时间没有发生。

另一方面:

 FormsAuthentication.SetAuthCookie("username", false); 

这会将cookie设置为不持久。 当您关闭浏览器时,cookie将被删除,从而在下次加载时强制登录,无论是否发生过期时间。 有关详细信息,请参阅FormsAuthentication.SetAuthCookie