ASP.NET Core 2.0 LDAP Active Directory Authentication

后端 未结 3 1424
别那么骄傲
别那么骄傲 2020-11-27 11:47

I have found a lot of information from the past saying that LDAP authentication isn\'t enabled yet but you can get around that using third party packages. However, it seems

3条回答
  •  悲哀的现实
    2020-11-27 12:10

    According to #2089, it is only available in Windows Compatibility-Pack for .NET Core. I currently use Novell.Directory.Ldap.NETStandard.

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

    For authentication and authorization, we can use Cookie Authentication Middleware with claims.

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, 
       ILoggerFactory loggerFactory)
    {
       app.UseCookieAuthentication(new CookieAuthenticationOptions
       {                
          AuthenticationScheme = "AuthenticationScheme",
          LoginPath = new PathString("/Account/Login"),
          AccessDeniedPath = new PathString("/Common/AccessDenied"),
          AutomaticAuthenticate = true,
          AutomaticChallenge = true
       });
    }
    

    It has few moving pieces, so I created a working sample project at GitHub. There are two main pieces - LdapAuthenticationService and SignInManager.

提交回复
热议问题