diff --git a/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/ldap/LdapConnection.Linux.cs b/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/ldap/LdapConnection.Linux.cs index 6383c1c0446a5..c4f58d928d7d9 100644 --- a/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/ldap/LdapConnection.Linux.cs +++ b/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/ldap/LdapConnection.Linux.cs @@ -5,6 +5,7 @@ using System.Diagnostics; using System.Net; using System.Runtime.InteropServices; +using System.Text; namespace System.DirectoryServices.Protocols { @@ -24,17 +25,26 @@ private int InternalConnectToServer() private int InternalBind(NetworkCredential tempCredential, SEC_WINNT_AUTH_IDENTITY_EX cred, BindMethod method) { - int error; if (tempCredential == null && (AuthType == AuthType.External || AuthType == AuthType.Kerberos)) { - error = BindSasl(); + return BindSasl(); } - else + + if (tempCredential != null) { - error = Interop.ldap_simple_bind(_ldapHandle, cred.user, cred.password); + var tempDomainName = new StringBuilder(100); + if (!string.IsNullOrEmpty(cred.domain)) + { + tempDomainName.Append(cred.domain); + tempDomainName.Append('\\'); + } + + tempDomainName.Append(cred.user); + return Interop.ldap_simple_bind(_ldapHandle, tempDomainName.ToString(), cred.password); } - return error; + return Interop.ldap_simple_bind(_ldapHandle, cred.user, cred.password); + } private int BindSasl() diff --git a/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/ldap/LdapConnection.Windows.cs b/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/ldap/LdapConnection.Windows.cs index 6ae5eb96143f6..935f4aef9a37c 100644 --- a/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/ldap/LdapConnection.Windows.cs +++ b/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/ldap/LdapConnection.Windows.cs @@ -4,6 +4,7 @@ using System.Diagnostics; using System.Net; +using System.Text; namespace System.DirectoryServices.Protocols { @@ -38,6 +39,25 @@ private int InternalConnectToServer() } private int InternalBind(NetworkCredential tempCredential, SEC_WINNT_AUTH_IDENTITY_EX cred, BindMethod method) - => tempCredential == null && AuthType == AuthType.External ? Interop.ldap_bind_s(_ldapHandle, null, null, method) : Interop.ldap_bind_s(_ldapHandle, null, cred, method); + { + if (tempCredential != null && AuthType == AuthType.Basic) + { + var tempDomainName = new StringBuilder(100); + if (!string.IsNullOrEmpty(cred.domain)) + { + tempDomainName.Append(cred.domain); + tempDomainName.Append('\\'); + } + + tempDomainName.Append(cred.user); + return Interop.ldap_simple_bind_s(_ldapHandle, tempDomainName.ToString(), cred.password); + } + + if (tempCredential == null && AuthType == AuthType.External) + return Interop.ldap_bind_s(_ldapHandle, null, null, method); + + return Interop.ldap_bind_s(_ldapHandle, null, cred, method); + } + } } diff --git a/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/ldap/LdapConnection.cs b/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/ldap/LdapConnection.cs index c66383e6d8a97..e1a61c13e75c9 100644 --- a/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/ldap/LdapConnection.cs +++ b/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/ldap/LdapConnection.cs @@ -2,7 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System.Globalization; using System.Net; using System.Collections; using System.ComponentModel; @@ -12,7 +11,6 @@ using System.Xml; using System.Threading; using System.Security.Cryptography.X509Certificates; -using System.Diagnostics.CodeAnalysis; namespace System.DirectoryServices.Protocols { @@ -1100,18 +1098,6 @@ private void BindHelper(NetworkCredential newCredential, bool needSetCredential) { error = LdapPal.BindToDirectory(_ldapHandle, null, null); } - else if (AuthType == AuthType.Basic) - { - var tempDomainName = new StringBuilder(100); - if (domainName != null && domainName.Length != 0) - { - tempDomainName.Append(domainName); - tempDomainName.Append('\\'); - } - - tempDomainName.Append(username); - error = LdapPal.BindToDirectory(_ldapHandle, tempDomainName.ToString(), password); - } else { var cred = new SEC_WINNT_AUTH_IDENTITY_EX() @@ -1129,42 +1115,26 @@ private void BindHelper(NetworkCredential newCredential, bool needSetCredential) if (tempCredential != null) { cred.user = username; - cred.userLength = (username == null ? 0 : username.Length); + cred.userLength = username?.Length ?? 0; cred.domain = domainName; - cred.domainLength = (domainName == null ? 0 : domainName.Length); + cred.domainLength = domainName?.Length ?? 0; cred.password = password; - cred.passwordLength = (password == null ? 0 : password.Length); - } - - BindMethod method = BindMethod.LDAP_AUTH_NEGOTIATE; - switch (AuthType) - { - case AuthType.Negotiate: - method = BindMethod.LDAP_AUTH_NEGOTIATE; - break; - case AuthType.Kerberos: - method = BindMethod.LDAP_AUTH_NEGOTIATE; - break; - case AuthType.Ntlm: - method = BindMethod.LDAP_AUTH_NTLM; - break; - case AuthType.Digest: - method = BindMethod.LDAP_AUTH_DIGEST; - break; - case AuthType.Sicily: - method = BindMethod.LDAP_AUTH_SICILY; - break; - case AuthType.Dpa: - method = BindMethod.LDAP_AUTH_DPA; - break; - case AuthType.Msn: - method = BindMethod.LDAP_AUTH_MSN; - break; - case AuthType.External: - method = BindMethod.LDAP_AUTH_EXTERNAL; - break; + cred.passwordLength = password?.Length ?? 0; } + var method = AuthType switch + { + AuthType.Negotiate => BindMethod.LDAP_AUTH_NEGOTIATE, + AuthType.Kerberos => BindMethod.LDAP_AUTH_NEGOTIATE, + AuthType.Ntlm => BindMethod.LDAP_AUTH_NTLM, + AuthType.Digest => BindMethod.LDAP_AUTH_DIGEST, + AuthType.Sicily => BindMethod.LDAP_AUTH_SICILY, + AuthType.Dpa => BindMethod.LDAP_AUTH_DPA, + AuthType.Msn => BindMethod.LDAP_AUTH_MSN, + AuthType.External => BindMethod.LDAP_AUTH_EXTERNAL, + _ => BindMethod.LDAP_AUTH_NEGOTIATE + }; + error = InternalBind(tempCredential, cred, method); }