如何在 asp.net 应用程序中注销按钮 Windows 身份验证
How to Logout button Windows Authentication in asp.net application
在我的项目中,我使用 Windows 身份验证登录。
需要注销按钮。
如果单击注销按钮页面应重定向到 Logout.aspx。
在 Logout.aspx 中,如果我在重定向回的浏览器中按下后退按钮。
如何控制不应该重定向到注销页面并要求 Window 身份验证登录?
在 windows 身份验证中,无法注销,因为您没有使用 IIS 进行身份验证。您正在针对 OS 使用它,即使您在同一个浏览器中注销,然后在下一个请求中您将自动在同一个浏览器中登录。
所以在windows auhtenication 中不可能注销。
在stack overflow中看到同类问题。
ASP.NET Windows Authentication logout
在每个 .aspx 页面中使用此脚本
<script type = "text/javascript" >
function changeHashOnLoad() {
window.location.href += "#";
setTimeout("changeHashAgain()", "50");
}
function changeHashAgain() {
window.location.href += "1";
}
var storedHash = window.location.hash;
window.setInterval(function () {
if (window.location.hash != storedHash) {
window.location.hash = storedHash;
}
}, 50);
</script>
我有一个 Web 表单解决方案,你可以使用它,希望对你有用。
logout.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="logout.aspx.cs" Inherits="logout" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="0">
</head>
<body>
<script type="text/javascript">
function HandleResult(arg, context) {
window.location = "/Login.aspx";
}
</script>
<form id="form1" runat="server">
</form>
<script>
CallServer('LoGout', '');
var Backlen=history.length;
history.go(-Backlen);
window.location.href = "/Login.aspx";
</script>
</body>
</html>
logout.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class logout : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
{
public void RaiseCallbackEvent(string eventArgument)
{
}
public string GetCallbackResult()
{
return "";
}
protected void Page_Load(object sender, EventArgs e)
{
ClearAll();
ClientScriptManager cm = Page.ClientScript;
string cbReference = cm.GetCallbackEventReference(this, "arg", "HandleResult", "");
string cbScript = "function CallServer(arg, context){" + cbReference + ";}";
cm.RegisterClientScriptBlock(this.GetType(), "CallServer", cbScript, true);
cm.RegisterStartupScript(this.GetType(), "cle", "windows.history.clear", true);
Response.Redirect("/login.aspx");
}
protected void Page_Init(object sender, EventArgs e)
{
ClearAll();
}
void ClearAll()
{
Session.RemoveAll();
System.Web.Security.FormsAuthentication.SignOut();
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
Response.Cache.SetNoStore();
}
}
我的项目中有这个,工作正常。
在我的项目中,我使用 Windows 身份验证登录。 需要注销按钮。
如果单击注销按钮页面应重定向到 Logout.aspx。 在 Logout.aspx 中,如果我在重定向回的浏览器中按下后退按钮。
如何控制不应该重定向到注销页面并要求 Window 身份验证登录?
在 windows 身份验证中,无法注销,因为您没有使用 IIS 进行身份验证。您正在针对 OS 使用它,即使您在同一个浏览器中注销,然后在下一个请求中您将自动在同一个浏览器中登录。
所以在windows auhtenication 中不可能注销。
在stack overflow中看到同类问题。
ASP.NET Windows Authentication logout
在每个 .aspx 页面中使用此脚本
<script type = "text/javascript" >
function changeHashOnLoad() {
window.location.href += "#";
setTimeout("changeHashAgain()", "50");
}
function changeHashAgain() {
window.location.href += "1";
}
var storedHash = window.location.hash;
window.setInterval(function () {
if (window.location.hash != storedHash) {
window.location.hash = storedHash;
}
}, 50);
</script>
我有一个 Web 表单解决方案,你可以使用它,希望对你有用。
logout.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="logout.aspx.cs" Inherits="logout" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="0">
</head>
<body>
<script type="text/javascript">
function HandleResult(arg, context) {
window.location = "/Login.aspx";
}
</script>
<form id="form1" runat="server">
</form>
<script>
CallServer('LoGout', '');
var Backlen=history.length;
history.go(-Backlen);
window.location.href = "/Login.aspx";
</script>
</body>
</html>
logout.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class logout : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
{
public void RaiseCallbackEvent(string eventArgument)
{
}
public string GetCallbackResult()
{
return "";
}
protected void Page_Load(object sender, EventArgs e)
{
ClearAll();
ClientScriptManager cm = Page.ClientScript;
string cbReference = cm.GetCallbackEventReference(this, "arg", "HandleResult", "");
string cbScript = "function CallServer(arg, context){" + cbReference + ";}";
cm.RegisterClientScriptBlock(this.GetType(), "CallServer", cbScript, true);
cm.RegisterStartupScript(this.GetType(), "cle", "windows.history.clear", true);
Response.Redirect("/login.aspx");
}
protected void Page_Init(object sender, EventArgs e)
{
ClearAll();
}
void ClearAll()
{
Session.RemoveAll();
System.Web.Security.FormsAuthentication.SignOut();
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
Response.Cache.SetNoStore();
}
}
我的项目中有这个,工作正常。