【问题标题】:How can you use asp.net core windows authentication alongside Cookie Authentication?如何在 Cookie 身份验证的同时使用 asp.net core windows 身份验证?
【发布时间】:2017-12-31 19:26:52
【问题描述】:

我有一个 ASP.net Core (.net framework 4.7) 应用程序,它使用 cookie 身份验证,就像 this link 中的一样

    app.UseCookieAuthentication(new CookieAuthenticationOptions()
    {
        AuthenticationScheme = "CookieAuthentication",
        LoginPath = new PathString("/Login/"),
        AccessDeniedPath = new PathString("/Login/"),
        AutomaticAuthenticate = true,
        AutomaticChallenge = true
    });

我想要做的是允许 Windows 身份验证和 Cookie 身份验证。因此,如果用户在公司的域中,他/她不必输入用户名和密码。但如果他们来自外部域,他们会被重定向到登录页面并输入他们的用户名和密码以进行身份​​验证.

【问题讨论】:

  • 目前如何认证外域用户?
  • 我通过 cookie 身份验证来做到这一点。

标签: asp.net-core active-directory asp.net-core-mvc windows-authentication asp.net-core-middleware


【解决方案1】:

如果用户在公司的域中,他/她不必输入 用户名和密码。

doesn't have to enter user name and password 是棘手的部分。据我所知,您不能同时满足两个身份验证

但是,您可以要求这两种类型的用户输入用户名和密码,然后先通过您的系统进行身份验证。如果身份验证失败,您可以使用域帐户进行身份验证。

如果该方案可行,您可以在 ASP.NET Core 中使用Novell.Directory.LdapHere 是示例代码。

public bool ValidateUser(string domainName, string username, string password)
{
   string userDn = $"{username}@{domainName}";
   try
   {
      using (var connection = new LdapConnection {SecureSocketLayer = false})
      {
         connection.Connect(domainName, LdapConnection.DEFAULT_PORT);
         connection.Bind(userDn, password);

         if (connection.Bound)
            return true;
      }
   }
   catch (LdapException ex)
   {
      // Log exception
   }
   return false;
}

注意:截至今天,System.DirectoryServices 在 ASP.NET Core 中尚不可用。

【讨论】:

  • 我想我可以将 app.UseCookieAuthentication 放在 app.UseMvc 之后,并启用 Windows 身份验证和匿名身份验证,如果 Windows 身份验证不起作用,管道将继续到达 CookieAuthentication。在时间允许我的登录控制器被访问 [AllowAnonymous] 和控制器的其余部分具有 [Authorize] 属性。有没有可能?谢谢,
  • 你也许可以把这两个中间件放在一起。我从未尝试过,所以我不能确定。
猜你喜欢
  • 2017-03-30
  • 2018-02-24
  • 2017-11-30
  • 2018-12-30
  • 2018-09-02
  • 1970-01-01
  • 2011-08-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多