diff --git a/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/Interop/LdapPal.Linux.cs b/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/Interop/LdapPal.Linux.cs index 4197abb9c1fd07..329237f10b3065 100644 --- a/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/Interop/LdapPal.Linux.cs +++ b/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/Interop/LdapPal.Linux.cs @@ -111,7 +111,7 @@ internal static int BindToDirectory(ConnectionHandle ld, string who, string pass passwordPtr = LdapPal.StringToPtr(passwd); BerVal passwordBerval = new BerVal { - bv_len = passwd.Length, + bv_len = passwd?.Length ?? 0, bv_val = passwordPtr, }; diff --git a/src/libraries/System.DirectoryServices.Protocols/tests/LdapConnectionTests.cs b/src/libraries/System.DirectoryServices.Protocols/tests/LdapConnectionTests.cs index 9a8074bf5222ad..a0522a61abc182 100644 --- a/src/libraries/System.DirectoryServices.Protocols/tests/LdapConnectionTests.cs +++ b/src/libraries/System.DirectoryServices.Protocols/tests/LdapConnectionTests.cs @@ -112,6 +112,16 @@ public void AuthType_SetValid_GetReturnsExpected() Assert.Equal(AuthType.Basic, connection.AuthType); } + [Fact] + public void AuthType_Anonymous_DoesNotThrowNull() + { + var connection = new LdapConnection("server"); + connection.AuthType = AuthType.Anonymous; + // When calling Bind we make sure that the exception thrown is not that there was a NullReferenceException + // trying to retrive a null password's lenght, but instead an LdapException given the server cannot be reached. + Assert.Throws(() => connection.Bind()); + } + [Theory] [InlineData(AuthType.Anonymous - 1)] [InlineData(AuthType.Kerberos + 1)]