Skip to content

Commit 43515e5

Browse files
Avoid exception when parsing AD path for port number (#110224)
Co-authored-by: Steve Harter <steveharter@users.noreply.github.com>
1 parent 51fd1e2 commit 43515e5

File tree

1 file changed

+16
-3
lines changed
  • src/libraries/System.DirectoryServices.AccountManagement/src/System/DirectoryServices/AccountManagement/AD

1 file changed

+16
-3
lines changed

src/libraries/System.DirectoryServices.AccountManagement/src/System/DirectoryServices/AccountManagement/AD/ADStoreCtx.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2405,6 +2405,9 @@ protected enum StoreCapabilityMap
24052405
// Must be called inside of lock(domainInfoLock)
24062406
protected virtual void LoadDomainInfo()
24072407
{
2408+
const int LdapDefaultPort = 389;
2409+
const int LdapsDefaultPort = 636;
2410+
24082411
GlobalDebug.WriteLineIf(GlobalDebug.Info, "ADStoreCtx", "LoadComputerInfo");
24092412

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

24202423
// Pull the requested port number
2421-
Uri ldapUri = new Uri(this.ctxBase.Path);
2422-
int port = ldapUri.Port != -1 ? ldapUri.Port : (ldapUri.Scheme.ToUpperInvariant() == "LDAPS" ? 636 : 389);
2424+
int port = LdapDefaultPort;
2425+
if (Uri.TryCreate(ctxBase.Path, UriKind.Absolute, out Uri ldapUri))
2426+
{
2427+
if (ldapUri.Port != -1)
2428+
{
2429+
port = ldapUri.Port;
2430+
}
2431+
else if (string.Equals(ldapUri.Scheme, "LDAPS", StringComparison.OrdinalIgnoreCase))
2432+
{
2433+
port = LdapsDefaultPort;
2434+
}
2435+
}
24232436

24242437
string dnsDomainName = "";
24252438

2426-
using (DirectoryEntry rootDse = new DirectoryEntry("LDAP://" + this.dnsHostName + ":" + port + "/rootDse", "", "", AuthenticationTypes.Anonymous))
2439+
using (DirectoryEntry rootDse = new DirectoryEntry($"LDAP://{this.dnsHostName}:{port}/rootDse", "", "", AuthenticationTypes.Anonymous))
24272440
{
24282441
this.defaultNamingContext = (string)rootDse.Properties["defaultNamingContext"][0];
24292442
this.contextBasePartitionDN = this.defaultNamingContext;

0 commit comments

Comments
 (0)