Skip to content

Querying a DC via System.DirectoryServices.Protocols hangs on macOS #87589

Open
@dlnash18

Description

@dlnash18

Description

I have PowerShell code to query a DC via LDAP that hangs in .NET 7.0.102 on macOS Ventura. This has been happening for at least a year across several versions of .NET and macOS. Almost identical code, differing only in SessionOptions.StartTransportLayerSecurity($null) instead of SessionOptions.SecureSocketLayer = $true and targeting OpenLDAP instead of AD, works fine. And the code that hangs on macOS works fine in .NET 7.0.304 on Windows.

The hang happens in System.DirectoryServices.Protocols.LdapConnection.SendRequest. Calling LdapConnection.Bind first does not hang, but it also does not prevent SendRequest from hanging.

Reproduction Steps

$LdapServer     = 'host.name.of.a.domain.controller'
$LdapPort       = 636
$LdapDirID      = New-Object System.DirectoryServices.Protocols.LdapDirectoryIdentifier($LdapServer, $LdapPort, $true, $false)                                                      

$Credential     = Get-Credential
$LdapCred       = New-Object System.Net.NetworkCredential($Credential.Username, $Credential.Password)                                                                               
$LdapAuthType   = [System.DirectoryServices.Protocols.AuthType]'Basic'

$LdapConnection = New-Object System.DirectoryServices.Protocols.LdapConnection($LdapDirID, $LdapCred, $LdapAuthType)                                                              
$LdapConnection.SessionOptions.SecureSocketLayer = $true

$LdapBase       = 'dc=abc,dc=xyz,dc=edu'
$LdapFilter     = '(samaccountname=MeMyselfAndI)'
$LdapScope      = [System.DirectoryServices.Protocols.SearchScope]'Subtree'
$SearchRequest  = New-Object System.DirectoryServices.Protocols.SearchRequest($LdapBase, $LdapFilter, $LdapScope)                                                                  

$LdapConnection.Bind()
$SearchResponse = $LdapConnection.SendRequest($SearchRequest) # Hangs forever

Changing these three lines makes it work:

$LdapServer     = 'host.name.of.an.OpenLDAP.server'
$LdapPort       = 389
$LdapConnection.SessionOptions.StartTransportLayerSecurity($null)

Expected behavior

I get a response from the LDAP server.

Actual behavior

LdapConnection.SendRequest hangs so badly that ^C won't stop it. I have to close the terminal window, which I have verified does kill the process (three cheers for SIGHUP).

Regression?

It has never worked in .NET 7 in my observation, but I don't claim to have tested every version.

Known Workarounds

No workaround.

Configuration

.NET SDK:
 Version:   7.0.102
 Commit:    4bbdd14480

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  13.4
 OS Platform: Darwin
 RID:         osx.13-x64

PowerShell 7.3.1

Earlier versions of both .NET 7 and macOS exhibit the problem. .NET 7 on Windows does not, at least not as of:

.NET SDK:
 Version:   7.0.304
 Commit:    7e794e2806

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.17763
 OS Platform: Windows
 RID:         win10-x64

PowerShell 7.3.4

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions