ASP.NET Windows身份验证注销

问题描述 投票:46回答:7

如何在ASP.NET中使用Windows身份验证时注销,如此web.config?

<authentication mode="Windows" />

我已经尝试过以下尝试失败了。它重定向,但不会注销用户。

void logoutButton_Click(object sender, EventArgs e) {
    HttpContext.Current.Session.Clear();
    HttpContext.Current.Session.Abandon();
    ViewState.Clear();
    FormsAuthentication.SignOut();
    Response.Redirect("/");
}

背景资料:

我必须使用Windows身份验证,因为我需要使用Active Directory模拟身份以获取对本地文件的访问权限。而且我无法模仿使用Forms身份验证,因为HttpContext.Current.User.Identity不会是WindowsIdentityImpersonate using Forms Authentication

asp.net windows-authentication logout
7个回答
32
投票

使用“Windows”身份验证时,服务器端注销按钮不起作用。如果要注销按钮,则必须使用“表单”身份验证,或关闭用户的浏览器。


21
投票

仅对于IE浏览器,如果使用Windows身份验证,则可以使用以下javascript注销用户。 (注意:不需要关闭浏览器,但建议使用浏览器,因为用户可能正在使用非IE浏览器)。

如果用户单击“否”关闭浏览器,则如果用户尝试访问需要身份验证的站点上的页面,则会提示用户输入用户名/密码。

try {
   document.execCommand("ClearAuthenticationCache");
}
catch (e) { }
window.close();

此代码取自SharePoint的Signout.aspx页面。


12
投票

Windows身份验证通过传递Windows身份验证令牌在IIS级别工作。由于身份验证发生在IIS级别,因此无法实际从应用程序代码注销。但是,似乎有一个问题here的答案。这是第二个问题,主要涉及使用表单身份验证和LogonUser Windows API。


6
投票

我有一个带有Windows身份验证的SharePoint应用程序,我需要在15分钟后自动注销。我混淆了一些代码,这是结果。它在IE中正常工作。

<script type="text/javascript">
var t;
window.onload = resetTimer;
document.onmousemove = resetTimer;
document.onkeypress = resetTimer;

function logout() {

    try {
        document.execCommand("ClearAuthenticationCache");
        window.location.href = window.location.protocol.replace(/\:/g, '') + "://" + window.location.host + "/_layouts/customlogin14.aspx";
    }
    catch (e) { }

}

function resetTimer() {
    window.clearTimeout(t);
    t = window.setTimeout(logout, 900000);
} 

将这些代码放在您的母版页中,闲置15分钟后您将看到登录页面。希望这对某人有所帮助


3
投票

我在IE和Firefox中都使用JavaScript,但它会记录您在IE中登录的所有内容。它在Safari中有效,但Safari会引发网络钓鱼警告。在Opera中不起作用。

    try { 
        if (document.all) 
        { 
            document.execCommand("ClearAuthenticationCache"); 
            window.location = "/"; 
        } 
        else 
        { 
            window.location = "http://logout:[email protected]"; 
        } 
    } 
    catch(e) 
    { 
        alert("It was not possible to clear your credentials from browser cache. Please, close your browser window to ensure that you are completely logout of system."); 
        self.close(); 
    } 


1
投票

我见过的最佳答案可以在相关的StackOverFlow问题中找到:

Is there a browser equivalent to IE's ClearAuthenticationCache?

Logging a user out when using HTTP Basic authentication

基本上,您需要使用无效凭据向服务器发送AJAX请求,并让服务器接受它们。


1
投票

有这么多麻烦,下面是有效的代码,希望有人发现它有用。

foreach (var cookie in Request.Cookies.Keys)
{
    Response.Cookies.Delete(cookie);
}


await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);


Response.Cookies.Append("EdgeAccessCookie", "", new Microsoft.AspNetCore.Http.CookieOptions()
{
    Path = "/",
    HttpOnly = true,
    SameSite = SameSiteMode.Lax, Expires = DateTime.Now.AddDays(-1)
});


Response.Redirect("https://adfs.[sitename].com/adfs/ls?wa=wsignout1.0");
© www.soinside.com 2019 - 2024. All rights reserved.