ASP.NET Core 2.0 LDAP Active Directory Authentication

后端 未结 3 1413
别那么骄傲
别那么骄傲 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:13

    The LDAP Authentication can be achieved using System.DirectoryServices.Protocols namespace.

    public Boolean IsAuthenticated(string username, string password,string domain)
    {
        Boolean authenticated = false;
        //pass the connectionString here
        using (LdapConnection connection = new LdapConnection(connectionString))
        {
           try
           {
               username = username + domain;
               connection.AuthType = AuthType.Basic;
               connection.SessionOptions.ProtocolVersion = 3;
               var credential = new NetworkCredential(username, password);
               connection.Bind(credential);
               authenticated = true;
               return authenticated;
           }
           catch (LdapException)
           {
               return authenticated;
           }
           finally
           {
               connection.Dispose();
           }
       }}
    

提交回复
热议问题