当浏览器关闭时,自动从ASP.NET的表单认证中签出

 2023-02-16    331  

问题描述

是否有一种方法可以强制ASP.NET在关闭浏览器或新地址中的用户类型时从其身份验证中登录?

?

当浏览器关闭时,自动从ASP.NET的表单认证中签出

如果浏览器打开打开,则我需要保持用户身份验证,因此最好在身份验证票上进行长时间的超时时间.

推荐答案

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

其他推荐答案

我已经从事此工作已经一段时间了,阅读并查看了各种帖子和答案,猜想和猜测.

此解决方案确实有一个重要的警告 – 弹出窗口(“烧死他!”我听到你哭了),JavaScript也是如此.但是,鉴于您需要如此安全地实施,我会害怕猜测用户会接受它以维持安全性,并且您可以为JavaScript启用编码.

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

的指令

in 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 file (文件” 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";
        };
    }
});

最后, popupcheck.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>

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

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

因此,在这一点上,我们现在已经”强迫”用户启用了本网站的弹出窗口 – 如所说,网站内容在我看来应该证明这种烦恼是合理的

步骤2-检查他们要去的地方,如果它们不在,则弹出logoff

在您的主要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并必须启用弹出窗口.

其他推荐答案

似乎您可以设置长时间的到期时间,并且也设定不坚持以获取您似乎正在寻找的确切行为.例如:

FormsAuthentication.SetAuthCookie("username", true);

这将其设置为持久,以便当您关闭浏览器时,它将保留cookie,以便即使他们关闭浏览器并再次加载您的网站,也可以使其不必登录这么长时间由于没有发生到期时间.

另一方面:

FormsAuthentication.SetAuthCookie("username", false);

这将cookie设置为不持久.关闭浏览器时,cookie将被删除,因此无论是否发生到期时间,下次加载它时都会强制登录.有关更多信息,请参见 FormsAuthentication.SetAuthCookie

以上所述是小编给大家介绍的当浏览器关闭时,自动从ASP.NET的表单认证中签出,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!

原文链接:https://77isp.com/post/33988.html

=========================================

https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。