C# 关闭浏览器时自动从ASP.NET中的窗体身份验证注销
当浏览器关闭或用户键入新地址时,是否有办法强制ASP.NET从其身份验证注销C# 关闭浏览器时自动从ASP.NET中的窗体身份验证注销,c#,jquery,asp.net,asp.net-membership,forms-authentication,C#,Jquery,Asp.net,Asp.net Membership,Forms Authentication,当浏览器关闭或用户键入新地址时,是否有办法强制ASP.NET从其身份验证注销 如果浏览器保持打开状态,那么我需要对用户进行身份验证,因此最好在身份验证单上长时间超时。我已经为此工作了一段时间,四处阅读并看到各种帖子和答案、猜测和推测 这个解决方案确实有一个重要的警告——弹出窗口(“烧死他!!!”我听到你哭了)是必需的,就像JavaScript一样。然而,考虑到您需要如此安全地实现这一点,我猜用户会接受这一点来维护安全性,并且您可以为启用JavaScript编写代码 步骤1-验证弹出窗口是否已启用
如果浏览器保持打开状态,那么我需要对用户进行身份验证,因此最好在身份验证单上长时间超时。我已经为此工作了一段时间,四处阅读并看到各种帖子和答案、猜测和推测 这个解决方案确实有一个重要的警告——弹出窗口(“烧死他!!!”我听到你哭了)是必需的,就像JavaScript一样。然而,考虑到您需要如此安全地实现这一点,我猜用户会接受这一点来维护安全性,并且您可以为启用JavaScript编写代码 步骤1-验证弹出窗口是否已启用-如果未启用,请重定向至如何启用的说明 在全球范围内。asax
<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>
设置会话变量以验证是否已选中弹出窗口:
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.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>
因此,现在我们已经“强制”用户启用该站点的弹出窗口-如前所述,在我看来,该站点的内容应该证明这种烦恼是合理的
第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
<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>
我们有一些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
};
};
最终限制重复 如果没有JavaScript或弹出窗口,这种方法将无法工作,至少对我来说,我将在一个安全性至关重要的受控环境中工作,因此这个解决方案值得最终用户的烦恼
如果你不得不走极端,那么我只能假设数据和应用程序足够敏感,迫使用户必须启用javascript和弹出窗口。我已经为此工作了一段时间,四处阅读并看到各种帖子和答案,猜测和猜测 这个解决方案确实有一个重要的警告——弹出窗口(“烧死他!!!”我听到你哭了)是必需的,就像JavaScript一样。然而,考虑到您需要如此安全地实现这一点,我猜用户会接受这一点来维护安全性,并且您可以为启用JavaScript编写代码 步骤1-验证弹出窗口是否已启用-如果未启用,请重定向至如何启用的说明 在全球范围内。asax
<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>
设置会话变量以验证是否已选中弹出窗口:
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.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>
因此,现在我们已经“强制”用户启用该站点的弹出窗口-如前所述,在我看来,该站点的内容应该证明这种烦恼是合理的
第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
<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>
我们有一些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
};
};
最终限制重复 如果没有JavaScript或弹出窗口,这种方法将无法工作,至少对我来说,我将在一个安全性至关重要的受控环境中工作,因此这个解决方案值得最终用户的烦恼
如果您必须达到这些极端目的,那么我只能假设数据和应用程序足够敏感,迫使用户必须启用javascript和弹出窗口。似乎您可以设置较长的过期时间,也可以将其设置为不持续,以获得您想要的确切行为。例如:
FormsAuthentication.SetAuthCookie("username", true);
这会将其设置为持久,以便在您关闭浏览器时,它将保留cookie,这样即使他们关闭浏览器并再次加载您的站点,它也会使cookie保持不变,以便他们不必登录,只要过期时间没有发生
另一方面:
FormsAuthentication.SetAuthCookie("username", false);
这会将cookie设置为不持久。当您关闭浏览器时,cookie将被删除,从而在下次加载cookie时强制登录,无论是否出现过期时间。有关更多信息,请参见您似乎可以设置较长的过期时间,并将其设置为不持续,以获得您想要的确切行为。例如:
FormsAuthentication.SetAuthCookie("username", true);
这会将其设置为持久,以便在您关闭浏览器时,它将保留cookie,这样即使他们关闭浏览器并再次加载您的站点,它也会使cookie保持不变,以便他们不必登录,只要过期时间没有发生
另一方面:
FormsAuthentication.SetAuthCookie("username", false);
这会将cookie设置为不持久。当您关闭浏览器时,cookie将被删除,从而在下次加载cookie时强制登录,无论是否出现过期时间。有关更多信息,请参见不确定这是否仍然是一个问题,但这为我解决了问题。只需将以下内容添加到起始页的页面加载事件中:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.UrlReferrer == null || string.IsNullOrEmpty(Request.UrlReferrer.AbsolutePath))
{
Session.Abandon();
FormsAuthentication.SignOut();
FormsAuthentication.RedirectToLoginPage();
}
}
不确定这是否仍然是一个问题,但该决议