2009-07-01 211 views
43

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

<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将不是WindowsIdentityImpersonate using Forms Authentication

+0

它似乎通过表单身份验证用户毕竟是可能的。见http://stackoverflow.com/a/11873754/359765 – bgh 2016-07-01 12:21:51

回答

29

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

11

Windows身份验证通过传递您的Windows身份验证令牌在IIS级别运行。由于身份验证发生在IIS级别,因此您不能实际从应用程序代码注销。但是,似乎有一个问题的答案here。这是解决的第二个问题,主要涉及使用Forms Authentication和LogonUser Windows API。

+0

太棒了!感谢这篇文章的链接。正是我想要的其他问题。请张贴我的其他问题,我会检查你的答案在那一个。 – Robert 2009-07-01 05:27:50

20

对于仅限IE浏览器,如果使用Windows身份验证,则可以使用以下JavaScript注销用户。 (注意:关闭浏览器不是必需的,但建议用户可能使用的是非IE浏览器)。

如果用户点击“否”关闭浏览器,则当用户尝试访问需要认证的网站上的网页时,系统会提示用户输入用户名/密码。

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

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

+0

太棒了!我希望这会在非IE浏览器中导致异常,因此在catch块中,我们可以向非IE用户显示警报,并提供进一步的说明。遗憾的是FF中有一个例外,但不是Chrome。那么,关闭窗户就够了吗?不确定。快速测试表明它可能在Chrome和FF中,但是我确信在IE(没有上述脚本)的情况下,在认证被清除之前需要关闭所有窗口。 – 2012-02-21 15:56:37

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(); 
    } 

6

我有一个SharePoint应用程序与Windows身份验证,我需要在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分钟的空闲时间后,你会看到登录页面。 希望这可以帮助某人