runtime: LdapSessionOptions.VerifyServerCertificate is not supported in non-Windows and error message is not helpful.
Description
LdapConnection fails to bind on Linux when running .NET 6.0.0-rc.2.21480.5 version of System.DirectoryServices.Protocols package and throws
Unhandled exception. System.DirectoryServices.Protocols.LdapException: The feature is not supported.
at System.DirectoryServices.Protocols.LdapConnection.BindHelper(NetworkCredential newCredential, Boolean needSetCredential)
at System.DirectoryServices.Protocols.LdapConnection.Bind()
The same code works when using switching to version 5.0.0 of System.DirectoryServices.Protocols or running under windows
Reproduction Steps
Run the following code under Linux
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.DirectoryServices.Protocols" Version="6.0.0-rc.2.21480.5" />
</ItemGroup>
</Project>
using System.DirectoryServices.Protocols;
using System.Net;
var di = new LdapDirectoryIdentifier(server: "ad.almirex.com", 389);
var connection = new LdapConnection(di, new NetworkCredential("username","password","almirex.dc"));
connection.Bind();
Console.WriteLine("Hello, World!");
If the package version is switched to 5.0.0, the above code works.
Expected behavior
The code works
Actual behavior
The code throws
Regression?
This worked in .NET 5 version of the package.
Known Workarounds
None
Configuration
Tested with .NET 6.0.100-rc.2.21505.57 SDK on WSL2 Ubuntu
Other information
No response
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 1
- Comments: 45 (25 by maintainers)
Commits related to this issue
- Force basic ldap auth to bypass .net 6 issue https://github.com/dotnet/runtime/issues/60972 — committed to NMica/NMica.Security by macsux 3 years ago
- added placeholder code for a workaround for the isssue: https://github.com/dotnet/runtime/issues/60972 — committed to MythicAgents/Athena by deleted user 2 years ago
Yes, we have several issues for trying to surface the error messages better, we can use this issue to track the work of making that better for .NET 7
This is most likely because .NET sets environment vars for a process in a separate layer to the actual process environment block. I came across this issue before when trying to set
KRB5_TRACE=/dev/stdout(or any of the other GSSAPI env vars) to debug some stuff and the C layer was unable to see these env vars.To set an env var at runtime for use by a called “PInvoke” function you need to essentially call
setenvinlibclikeYou would then do
Native.Libc.setenv("LDAPTLS_REQCERT", "never");to set the env var.@akamud We’ve found out that this issue was related to the wrong configurated LDAP server & missing certificates. Also, I found that specifying FQDN as server address in the LdapDirectoryIdentifier not working on the Linux platform, probably because of this. Also, there’s the VerifyServerCertificate callback in which you must validate your LDAP certificate, but this functionality not working on the Linux (because, on the Linux, we are basically only passing through the Bind to the underlying native library), and I’m not sure about Mac platform.
Here’s the code sample of the class that I wrote to manage LDAP connections (I changed the code a bit so as not to violate the police company I work with):
In the case of Mac, maybe you must verify the certificate yourself too (like on Win), so try to do some experiments with this sample.
@macsux I tried Your solution but got the same error as always:
I use the same server address string as for Windows:
var di = new LdapDirectoryIdentifier(server: "10.6.1.162", 636, true, false);Am I using wrong connection string? Maybe external connection to this IP address through 636 port is blocked, I’ll try to contact my DevOps.This code is confirmed to work https://github.com/NMica/NMica.Security/blob/3868ceab4880058fbca4b66d651a169d04757d1d/src/NMica.AspNetCore.Authentication.Spnego/Ldap/LdapRolesClaimsTransformation.cs#L204
On Thu., Mar. 17, 2022, 1:40 p.m. parrssee, @.***> wrote:
Also, I’m getting a
CA1416warning onconnection.SessionOptions.SecureSocketLayer = options.UseSsl;telling me this API is windows only. Is this a left over that forgot to be removed after LDAPS supported was added for Linux?