System.DirectoryServices.Protocols.LdapConnection has wrong argument check (#78459...
authorGeert van Horrik <GeertvanHorrik@users.noreply.github.com>
Fri, 3 Feb 2023 23:23:05 +0000 (00:23 +0100)
committerGitHub <noreply@github.com>
Fri, 3 Feb 2023 23:23:05 +0000 (15:23 -0800)
* #78459 System.DirectoryServices.Protocols.LdapConnection has wrong argument check for anonymous authentication

* Add unit tests for ldap-anonymous-auth-fix

src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/ldap/LdapConnection.cs
src/libraries/System.DirectoryServices.Protocols/tests/LdapConnectionTests.cs

index 6eb0321..8184c21 100644 (file)
@@ -1014,7 +1014,7 @@ namespace System.DirectoryServices.Protocols
             }
 
             // Throw if user wants to do anonymous bind but specifies credentials.
-            if (AuthType == AuthType.Anonymous && (newCredential != null && (!string.IsNullOrEmpty(newCredential.Password) || string.IsNullOrEmpty(newCredential.UserName))))
+            if (AuthType == AuthType.Anonymous && (newCredential != null && (!string.IsNullOrEmpty(newCredential.Password) || !string.IsNullOrEmpty(newCredential.UserName))))
             {
                 throw new InvalidOperationException(SR.InvalidAuthCredential);
             }
index a9b0788..413c617 100644 (file)
@@ -112,11 +112,17 @@ namespace System.DirectoryServices.Protocols.Tests
             Assert.Equal(AuthType.Basic, connection.AuthType);
         }
 
-        [Fact]
-        public void AuthType_Anonymous_DoesNotThrowNull()
+        public static IEnumerable<object[]> AuthType_Anonymous_DoesNotThrowNull_TestData()
         {
-            var connection = new LdapConnection("server");
-            connection.AuthType = AuthType.Anonymous;
+            yield return new object[] { new LdapDirectoryIdentifier("server"), null };
+            yield return new object[] { new LdapDirectoryIdentifier("server"), new NetworkCredential() };
+        }
+
+        [Theory]
+        [MemberData(nameof(AuthType_Anonymous_DoesNotThrowNull_TestData))]
+        public void AuthType_Anonymous_DoesNotThrowNull(LdapDirectoryIdentifier identifier, NetworkCredential credential)
+        {
+            var connection = new LdapConnection(identifier, credential, AuthType.Anonymous);
             // When calling Bind we make sure that the exception thrown is not that there was a NullReferenceException
             // trying to retrieve a null password's length, but instead an LdapException given the server cannot be reached.
             Assert.Throws<LdapException>(() => connection.Bind());