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

问题描述 投票:0回答:3

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

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

c# jquery asp.net asp.net-membership forms-authentication
3个回答
4
投票

不确定这是否仍然是一个问题,但这解决了我的问题。只需将以下内容添加到起始页的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();
    }
}

1
投票

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

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

步骤1 - 验证是否启用了弹出窗口 - 如果没有,请重定向到有关如何启用的说明

在Global.asax中

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

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("<script src=\"/js/popupCheck.js\" type=\"text/javascript\"></script>"));
    }
}

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

<head runat="server">
    <title>Popup Checker</title>
    <script language="javascript" type="text/javascript">
        window.close();
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Popup windows are enabled, please close this window.
    </div>
    </form>
</body>
</html>

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

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

所以在这一点上,我们现在已经“强迫”用户启用此网站的弹出窗口 - 如上所述,网站内容应证明我认为这种烦恼是合理的

第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
    };        
};

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

<asp:Button Text="logout..." runat="server" ID="cmdLogout" OnClick="cmdLogout_click" />

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

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并且必须启用弹出窗口。


1
投票

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

FormsAuthentication.SetAuthCookie("username", true); 

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

另一方面:

FormsAuthentication.SetAuthCookie("username", false); 

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

© www.soinside.com 2019 - 2024. All rights reserved.