Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ASP.NET Windows身份验证注销_Asp.net_Windows Authentication_Logout - Fatal编程技术网

ASP.NET Windows身份验证注销

ASP.NET Windows身份验证注销,asp.net,windows-authentication,logout,Asp.net,Windows Authentication,Logout,在ASP.NET中使用Windows身份验证(如此web.config)时,如何注销 <authentication mode="Windows" /> 背景资料: 我必须使用Windows身份验证,因为我需要使用Active Directory模拟身份才能访问本地文件。我无法使用表单身份验证进行模拟,因为HttpContext.Current.User.Identity将不是WindowsIdentity。 Windows身份验证通过传递Windows身份验证令牌在IIS级别工作

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

<authentication mode="Windows" />
背景资料:

我必须使用Windows身份验证,因为我需要使用Active Directory模拟身份才能访问本地文件。我无法使用表单身份验证进行模拟,因为
HttpContext.Current.User.Identity
将不是
WindowsIdentity

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

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

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

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

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

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

我在IE和Firefox中都使用JavaScript,尽管它会将您登录IE的所有内容都注销。它在Safari中可以正常工作,但Safari会发出钓鱼警告。在歌剧中不起作用

试试看{
如果(全部文件){
document.execCommand(“ClearAuthenticationCache”);
window.location=“/”;
}否则{
window.location=”http://logout:logout@例如:"example.com",;
}
}捕获(e){
警报(“无法从浏览器缓存中清除您的凭据。请关闭浏览器窗口以确保您已完全注销系统。”);
self.close();
}

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


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

我有一个具有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);
} 

变量t;
window.onload=重置计时器;
document.onmousemove=resetTimer;
document.onkeypress=重置计时器;
函数注销(){
试一试{
document.execCommand(“ClearAuthenticationCache”);
window.location.href=window.location.protocol.replace(/\:/g',)+“://”+window.location.host+“/\u layouts/customlogin14.aspx”;
}
捕获(e){}
}
函数resetTimer(){
窗口清除超时(t);
t=窗口设置超时(注销,900000);
} 

将这些代码放入母版页,15分钟空闲时间后,您将看到登录页面。
希望这能帮助那些有很多麻烦的人,下面是代码,希望有人觉得有用

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

我认为您应该使用forms auth,但您可以在如下表单中使用ldap windows用户帐户:

using(PrincipalContext pc = new PrincipalContext(ContextType.Domain, "YOURDOMAIN"))
{
    // validate the credentials
    bool isValid = pc.ValidateCredentials("myuser", "mypassword");
}

令人惊叹的!谢谢你给那篇文章的链接。这正是我想问的另一个问题。请把这个问题贴到我的另一个问题上,我会检查你是否回答了这个问题。太棒了!我希望这会在非IE浏览器中引起异常,以便在catch块中,我们可以向非IE用户显示警报,并提供进一步的说明。FF中有一个例外,但不幸的是Chrome中没有。那么,关上窗户就够了吗?不确定。快速测试表明它可能在Chrome和FF中,但我可以肯定地知道,使用IE(没有上面的脚本)时,在清除身份验证之前,需要关闭所有窗口。还值得指出的是,根据此链接,上面的命令清除所有身份验证数据,而不仅仅是针对请求它的站点!我将代码更改为重定向,如本文所述-看来通过表单身份验证模拟用户毕竟是可能的。看到这是假设通过Windows身份基础的ADFS…NTLM Windows Auth并不总是使用ADFS。仅使用基本NTLM auth,所有这些都不相关。
using(PrincipalContext pc = new PrincipalContext(ContextType.Domain, "YOURDOMAIN"))
{
    // validate the credentials
    bool isValid = pc.ValidateCredentials("myuser", "mypassword");
}