Skip to content

Commit

Permalink
Avoid exception when parsing AD path for port number (#110224)
Browse files Browse the repository at this point in the history
Co-authored-by: Steve Harter <[email protected]>
  • Loading branch information
github-actions[bot] and steveharter authored Dec 17, 2024
1 parent 51fd1e2 commit 43515e5
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 43515e5

Please sign in to comment.