【问题标题】:ASP.NET Windows Authentication logoutASP.NET Windows 身份验证注销
【发布时间】:2010-11-07 05:24:09
【问题描述】:

在像这样的 web.config 中使用 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("/");
}

背景信息:

我必须使用 Windows 身份验证,因为我需要使用 Active Directory 模拟身份来访问本地文件。而且我不能使用 Forms 身份验证来模拟,因为HttpContext.Current.User.Identity 不会是WindowsIdentityImpersonate using Forms Authentication

【问题讨论】:

标签: asp.net windows-authentication logout


【解决方案1】:

我认为你应该使用表单身份验证,但你可以在这样的表单中使用 ldap windows 用户帐户:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      遇到了很多麻烦,下面是有效的代码,希望有人觉得它有用。

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

      【讨论】:

      • 这是假设 ADFS 通过 Windows Identity Foundation.... NTLM Windows Auth 并不总是使用 ADFS。只使用基本的 NTLM 身份验证,这些都无关紧要。
      【解决方案4】:

      我有一个带有 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 分钟空闲时间后,您将看到登录页面。 希望这对某人有所帮助

      【讨论】:

        【解决方案5】:

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

        Is there a browser equivalent to IE's ClearAuthenticationCache?

        Logging a user out when using HTTP Basic authentication

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

        【讨论】:

          【解决方案6】:

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

          【讨论】:

            【解决方案7】:

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

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

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

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

            【讨论】:

            • 太棒了!我希望这会在非 IE 浏览器中导致异常,以便在 catch 块中我们可以向非 IE 用户显示警报并提供进一步的说明。 FF 有一个例外,但不幸的是 Chrome 没有。那么,关上窗户就够了吗?不确定。快速测试表明它可能在 Chrome 和 FF 中,但我确信使用 IE(没有上述脚本)在清除身份验证之前需要关闭所有窗口。
            • 还值得指出的是,根据此链接,上述命令将清除所有身份验证数据,而不仅仅是请求它的站点msdn.microsoft.com/en-us/library/ms536979.aspx
            • 太棒了!我将代码更改为重定向,如此处所述 - stackoverflow.com/questions/12742319/…
            【解决方案8】:

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

            【讨论】:

            • 太棒了!感谢您提供该文章的链接。正是我想要的另一个问题。请将其发布到我的另一个问题中,我会按照该问题的答案检查您。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2019-01-31
            • 2015-05-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-09-22
            • 1970-01-01
            相关资源
            最近更新 更多