-
Notifications
You must be signed in to change notification settings - Fork 5k
LdapConnection causes an NullReferenceException when authenticating with Authentication type external and a client certificate #113154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Tagging subscribers to this area: @dotnet/area-system-directoryservices, @jay98014 |
It looks to me like this exception might be coming from the interop source generated code (cc @elinor-fung) @Davidvanluijk can you please fill out the complete template indicating if this is a regression or if it never worked? Try the 6.0 package and also .NETFramework to see if they worked - both predate the use of the source generator. |
This issue has been marked |
I took a closer look at this today and was able to reproduce. It does seem to be a regression from this change ccd67b0#diff-25eb162cb88bf1dcd67cdf438cb303832bb8f16bbe5fa7266d9faabda8100922R41 @jkoritzinsky you changed this type from class to struct ccd67b0#diff-cd60d68ca0e274c1592c8edcce8c98cc4044670de44970b1affcad9616c377eaR33 But the marshaller doesn't account for null and it's passed in this case. I'm not too familiar with Lines 64 to 65 in aba4de8
runtime/src/libraries/Common/src/Interop/Interop.Ldap.cs Lines 34 to 81 in aba4de8
|
Yeah it looks like I didn't really account for the case that the struct was non-blittable and that we don't support null refs in that case. This type should be changed back to a class and the marshaller logic should change to handle allocating and freeing the Native struct as part of marshalling (and passing a null pointer for the null case) |
@jkoritzinsky Can you provide an example of that? I understand what to change with
|
I came up with what might be a simpler approach - just an overload of the PInvoke that will accept an IntPtr expected to be null. I'll give this a try and see if it works. @Davidvanluijk -- you can try 6.0.2 and see if it works as a temporary workaround. You can also try this private build with the proposed fix. System.DirectoryServices.Protocols.10.0.0-dev.nupkg.zip |
The simpler approach looks reasonable. We can just go with that. |
I can confirm this error does not occur with 6.0.2, haven't tested with the private build. As I'm not sure how this process works: Will this fix be available for .net 8 or only for .net 10? It will be released when .net 10 is released? |
First of all: thank you for the fast response on this ticket. After more testing with 6.0.2 I do have a follow up question/bug: the above code throws an exception "The authentication method is not supported". The LdapConnection code seems to be calling "ldap_bind_sw", but the winapi docs (https://learn.microsoft.com/en-us/windows/win32/api/winldap/nf-winldap-ldap_bind_sw) don't mention external as supported method. Is it correct to assume this doesn't work because of this reason? Could the alternative ldap_sasl_bind_s be more approriate in this case? |
@Davidvanluijk if possible can you verify your test against .NET Framework -- that code looks to be essentially the same as v6 which uses "ldap_bind_sw". |
I did see examples similar to this in other codebases, so I assume it should work. It's also been this way for ages - same in .NETFramework. I get the same result from this library after the fix, and from .NETFramework. I debugged winldap to see what's happening, the client library is not the source of the error - it successfully sends the LDAP request to the server. I see the server responds with a result of 7 Now I'm not sure if that's because the client formed the request wrong, or if indeed my AD server doesn't allow this type of auth, but at least it's consistent. We're not really the LDAP experts here so I'm not sure if we can get a better answer. @BRDPM @grubioe @jay98014 own this on Windows and may be able to help answer. I did find these docs -- https://learn.microsoft.com/en-us/entra/identity/domain-services/tutorial-configure-ldaps -- which imply server side settings might be required. |
For anyone stumbling across this issue and wondering how to get it to work, I posted the final working example on stackoverflow: |
Description
When calling the Bind method without credentials and with authentication type external a null reference is thrown.
In case an empty credential object is passed an exception is thrown (LDAP Authentication failed: System.DirectoryServices.Protocols.LdapException: A bad parameter was passed to a routine), so this is no useable alternative.
When we create a test application which does not use LdapConnection but calls the ldap_bind directly it does work with a null credential object.
Reproduction Steps
`using System.DirectoryServices.Protocols;
using System.Security.Cryptography.X509Certificates;
namespace X509Store;
public class SystemLdap
{
public static void Run(X509Certificate2 cert)
{
string ldapPath = "dc";
int LDAPPort = 636;
var username = "user";
}
`
Expected behavior
Bind succesfully completes
Actual behavior
LDAP Authentication failed: System.NullReferenceException: Object reference not set to an instance of an object.
at Interop.Ldap.ldap_bind_s(ConnectionHandle ldapHandle, String dn, SEC_WINNT_AUTH_IDENTITY_EX& credentials, BindMethod method)
at System.DirectoryServices.Protocols.LdapConnection.InternalBind(NetworkCredential tempCredential, SEC_WINNT_AUTH_IDENTITY_EX cred, BindMethod method)
at System.DirectoryServices.Protocols.LdapConnection.BindHelper(NetworkCredential newCredential, Boolean needSetCredential)
at System.DirectoryServices.Protocols.LdapConnection.Bind()
at X509Store.SystemLdap.Run(X509Certificate2 smartCardCert) in SystemLdap.cs:line 22
Regression?
No response
Known Workarounds
None
Configuration
x64 Net. 8.0 on a Windows 11 system
Using System.DirectoryServices.AccountManagement version 9.0.2
Other information
No response
The text was updated successfully, but these errors were encountered: