简体   繁体   English

如何在asp.net应用程序中注销按钮Windows身份验证

[英]How to Logout button Windows Authentication in asp.net application

In my Project I am using Windows Authentication Login.在我的项目中,我使用 Windows 身份验证登录。 Logout Button is Required.注销按钮是必需的。

If Click on Logout Button Page should be redirect to Logout.aspx.如果单击注销按钮页面应重定向到 Logout.aspx。 In Logout.aspx if I press Back Button in Browser that is redirect back.在 Logout.aspx 中,如果我按浏览器中的后退按钮重定向回来。

How To control should not redirect to back In LogOut Page and Ask for Window Authentication Login?如何控制不应该重定向回注销页面并要求窗口身份验证登录?

In windows authentication there is no possibility of logout as you are not using IIS for authentication.在 Windows 身份验证中,由于您没有使用 IIS 进行身份验证,因此无法注销。 You are using that against OS and even if you logout in same browser and then on next request you will automatically login in same browser.您正在针对操作系统使用它,即使您在同一浏览器中注销,然后在下一次请求时,您也会自动在同一浏览器中登录。

So there is no possibility of logout in windows auhtentication.所以在windows auhtentication中没有注销的可能性。

See same kind of question in stack overflow.在堆栈溢出中看到同样的问题。

ASP.NET Windows Authentication logout ASP.NET Windows 身份验证注销

I have a Web-form solution for this , you can use it , I hope be useful for you.我有一个Web表单解决方案,您可以使用它,希望对您有用。

logout.aspx登出.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登出.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();


    }
}

I Have this in my projects that working fine.我在我的项目中有这个工作正常。

Use This Script in every .aspx page在每个 .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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM