Skip to content

Commit

Permalink
Avoid exception when parsing AD path for port number (dotnet#109977)
Browse files Browse the repository at this point in the history
  • Loading branch information
steveharter authored and mikelle-rogers committed Dec 4, 2024
1 parent cb754ec commit f5e7b06
Showing 1 changed file with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2405,6 +2405,9 @@ protected enum StoreCapabilityMap
// Must be called inside of lock(domainInfoLock)
protected virtual void LoadDomainInfo()
{
const int LdapDefaultPort = 389;
const int LdapsDefaultPort = 636;

GlobalDebug.WriteLineIf(GlobalDebug.Info, "ADStoreCtx", "LoadComputerInfo");

Debug.Assert(this.ctxBase != null);
Expand All @@ -2418,12 +2421,22 @@ protected virtual void LoadDomainInfo()
this.dnsHostName = ADUtils.GetServerName(this.ctxBase);

// Pull the requested port number
Uri ldapUri = new Uri(this.ctxBase.Path);
int port = ldapUri.Port != -1 ? ldapUri.Port : (ldapUri.Scheme.ToUpperInvariant() == "LDAPS" ? 636 : 389);
int port = LdapDefaultPort;
if (Uri.TryCreate(ctxBase.Path, UriKind.Absolute, out Uri ldapUri))
{
if (ldapUri.Port != -1)
{
port = ldapUri.Port;
}
else if (string.Equals(ldapUri.Scheme, "LDAPS", StringComparison.OrdinalIgnoreCase))
{
port = LdapsDefaultPort;
}
}

string dnsDomainName = "";

using (DirectoryEntry rootDse = new DirectoryEntry("LDAP://" + this.dnsHostName + ":" + port + "/rootDse", "", "", AuthenticationTypes.Anonymous))
using (DirectoryEntry rootDse = new DirectoryEntry($"LDAP://{this.dnsHostName}:{port}/rootDse", "", "", AuthenticationTypes.Anonymous))
{
this.defaultNamingContext = (string)rootDse.Properties["defaultNamingContext"][0];
this.contextBasePartitionDN = this.defaultNamingContext;
Expand Down

0 comments on commit f5e7b06

Please sign in to comment.