@@ -31,7 +31,7 @@ internal DirectoryEntryBuilder(
3131 _password = credential ? . GetNetworkCredential ( ) . Password ;
3232 _authenticationTypes = authenticationTypes ;
3333 DomainEntry = Create ( server : server ) ;
34- SearchBase = searchBase is null ? DomainEntry : Create ( searchBase : searchBase ) ;
34+ SearchBase = ResolveSearchBase ( searchBase ) ;
3535 }
3636
3737 internal DirectoryEntry Create ( string ? server = null , string ? searchBase = null )
@@ -47,7 +47,31 @@ internal DirectoryEntry Create(string? server = null, string? searchBase = null)
4747 if ( path is not null && ! path . Contains ( "://" ) )
4848 path = $ "LDAP://{ path } ";
4949
50- return new DirectoryEntry ( path , _username , _password , _authenticationTypes ) ;
50+ DirectoryEntry entry = new ( path , _username , _password , _authenticationTypes ) ;
51+ _ = entry . NativeObject ; // force bind
52+ return entry ;
53+ }
54+
55+ private DirectoryEntry ResolveSearchBase ( string ? searchBase )
56+ {
57+ if ( searchBase is null ) return DomainEntry ;
58+
59+ if ( ! searchBase . Contains ( "=" ) )
60+ throw new ArgumentException (
61+ $ "SearchBase '{ searchBase } ' is not a valid DistinguishedName. " +
62+ "It must follow the format 'OU=Name,DC=domain,DC=com' for an Organizational Unit or Container." ,
63+ nameof ( searchBase ) ) ;
64+
65+ try
66+ {
67+ return Create ( searchBase : searchBase ) ;
68+ }
69+ catch ( Exception exception )
70+ {
71+ throw new ArgumentException (
72+ $ "SearchBase '{ searchBase } ' could not be found in '{ DomainDistinguishedName } '.",
73+ nameof ( searchBase ) , innerException : exception ) ;
74+ }
5175 }
5276
5377 public void Dispose ( )
0 commit comments