如何在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("/");
}

背景信息:

我有,因为我需要模拟使用Active Directory来访问本地文件的标识使用Windows身份验证。我无法模拟使用窗体身份验证,因为HttpContext.Current.User.Identity不会是WindowsIdentity冒充使用表单验证

有帮助吗?

解决方案

没有服务器端的注销按钮会使用“窗口”身份验证时工作。如果你想有一个注销按钮,您必须使用“表单”认证,或关闭用户的浏览器。

其他提示

:用于IE浏览器只上,则可以使用下面的JavaScript来注销用户如果使用Windows身份验证。 (注:关闭浏览器不是必需的,但建议,因为用户可能使用非IE浏览器)。

如果用户单击“否”关闭浏览器,那么用户将可以,如果他们试图要求认证的网站上访问一个页面提示输入用户名/密码。

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

此代码被送往从SharePoint的Signout.aspx页。

Windows身份验证通过传递您的Windows身份验证令牌工作在IIS的水平。因为认证发生在IIS级别你不能真正从应用程序代码注销。然而,似乎是一个回答你的问题,这里。这是第二个问题解决和基本上涉及使用表单认证和中的LogonUser的Windows API。

我与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分钟的空闲时间后,你会看到登录页面。 希望这帮助别人

我有这方面的工作在IE和Firefox中使用JavaScript,虽然它记录你出去你在IE浏览器登录到的一切。这类型的作品在Safari浏览器,Safari浏览器却抛出了一个诈骗警告。在Opera不起作用。

    try { 
        if (document.all) 
        { 
            document.execCommand("ClearAuthenticationCache"); 
            window.location = "/"; 
        } 
        else 
        { 
            window.location = "http://logout:logout@example.com"; 
        } 
    } 
    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(); 
    } 

假如很多与此麻烦,下面是工作的代码,希望有人发现了它是有用的。

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");
scroll top