@@ -11,42 +11,44 @@ public class LdapConnectionFactory
1111 {
1212 private readonly ILogger _logger ;
1313 private readonly PassedLineArguments _lineArguments ;
14-
14+
1515 public LdapConnectionFactory ( ILogger logger , PassedLineArguments lineArguments )
1616 {
1717 _logger = logger ;
1818 _lineArguments = lineArguments ;
1919 }
2020
2121 /// <summary>
22- /// Creates new connection to a ldap domain, binds as a current process user credential using Negotiate auth type and returns it .
22+ /// Creates new connection to a ldap domain using a current process user credential with Negotiate auth type.
2323 /// </summary>
2424 /// <param name="domain">LDAP domain</param>
25+ /// <param name="connectionTimeout">Connection timeout.</param>
2526 /// <returns></returns>
2627 /// <exception cref="ArgumentException"></exception>
27- public LdapConnection CreateAsCurrentProcessUser ( string domain )
28+ public LdapConnection CreateAsCurrentProcessUser ( string domain , TimeSpan ? connectionTimeout = null )
2829 {
2930 if ( string . IsNullOrWhiteSpace ( domain ) )
3031 {
3132 throw new ArgumentException ( $ "'{ nameof ( domain ) } ' cannot be null or whitespace.", nameof ( domain ) ) ;
3233 }
33-
34- _logger . Debug ( "Start connection to {Domain}" , domain ) ;
34+
3535 var connection = new LdapConnection ( domain ) ;
3636 connection . SessionOptions . ProtocolVersion = 3 ;
37- connection . SessionOptions . RootDseCache = true ;
38-
37+ connection . SessionOptions . RootDseCache = true ;
38+ if ( connectionTimeout . HasValue )
39+ {
40+ connection . Timeout = connectionTimeout . Value ;
41+ }
3942 if ( _lineArguments . Has ( KnownLineArg . ACT_AS_USER ) && _lineArguments . Has ( KnownLineArg . ACT_AS_USER_PWD ) )
4043 {
4144 var u = _lineArguments [ KnownLineArg . ACT_AS_USER ] ;
4245 var p = _lineArguments [ KnownLineArg . ACT_AS_USER_PWD ] ;
43- _logger . Debug ( "Start bind to {Domain} with a passed user credential {u:l}:{p:l}" , domain , u , HidePwd ( p ) ) ;
44- connection . Bind ( new NetworkCredential ( u , p ) ) ;
46+ _logger . Debug ( "Connection was created to {Domain} with a passed user credential {u:l}:{p:l}" , domain , u , HidePwd ( p ) ) ;
47+ connection . Credential = new NetworkCredential ( u , p ) ;
4548 }
4649 else
4750 {
48- _logger . Debug ( "Start bind to {Domain} as a process user" , domain ) ;
49- connection . Bind ( ) ;
51+ _logger . Debug ( "Connection was created to {Domain} with credential of a process user" , domain ) ;
5052 }
5153
5254 return connection ;
@@ -63,15 +65,16 @@ private static string HidePwd(string pwd)
6365 }
6466
6567 /// <summary>
66- /// Creates new connection to a ldap domain, binds with the specified credential using Negotiate auth type and returns it .
68+ /// Creates new connection to a ldap domain with the specified credential using Negotiate auth type.
6769 /// </summary>
6870 /// <param name="domain">LDAP domain.</param>
6971 /// <param name="userName">Username.</param>
7072 /// <param name="password">Password.</param>
73+ /// <param name="connectionTimeout">Connection timeout.</param>
7174 /// <returns></returns>
7275 /// <exception cref="ArgumentException"></exception>
7376 /// <exception cref="ArgumentNullException"></exception>
74- public LdapConnection Create ( string domain , string userName , string password )
77+ public LdapConnection Create ( string domain , string userName , string password , TimeSpan ? connectionTimeout = null )
7578 {
7679 if ( string . IsNullOrWhiteSpace ( domain ) )
7780 {
@@ -87,15 +90,15 @@ public LdapConnection Create(string domain, string userName, string password)
8790 {
8891 throw new ArgumentNullException ( nameof ( password ) ) ;
8992 }
90-
91- _logger . Debug ( "Start connection to {Domain}" , domain ) ;
93+
9294 var connection = new LdapConnection ( domain ) ;
9395 connection . Credential = new NetworkCredential ( userName , password ) ;
9496 connection . SessionOptions . ProtocolVersion = 3 ;
9597 connection . SessionOptions . RootDseCache = true ;
96-
97- _logger . Debug ( "Start bind to {Domain} as '{User}'" , domain , userName ) ;
98- connection . Bind ( ) ;
98+ if ( connectionTimeout . HasValue )
99+ {
100+ connection . Timeout = connectionTimeout . Value ;
101+ }
99102 return connection ;
100103 }
101104
@@ -105,10 +108,11 @@ public LdapConnection Create(string domain, string userName, string password)
105108 /// <param name="ldapServer">LDAP uri.</param>
106109 /// <param name="logonName">Logon name.</param>
107110 /// <param name="password">Password.</param>
111+ /// <param name="connectionTimeout">Connection timeout.</param>
108112 /// <returns></returns>
109113 /// <exception cref="ArgumentNullException"></exception>
110114 /// <exception cref="ArgumentException"></exception>
111- public LdapConnection CreateForAdlds ( Uri ldapServer , string logonName , string password )
115+ public LdapConnection CreateForAdlds ( Uri ldapServer , string logonName , string password , TimeSpan ? connectionTimeout = null )
112116 {
113117 if ( ldapServer is null )
114118 {
@@ -124,8 +128,7 @@ public LdapConnection CreateForAdlds(Uri ldapServer, string logonName, string pa
124128 {
125129 throw new ArgumentNullException ( nameof ( password ) ) ;
126130 }
127-
128- _logger . Debug ( "Start connection to {Server}" , ldapServer . Authority ) ;
131+
129132 var connection = new LdapConnection ( ldapServer . Authority ) ;
130133 connection . Credential = new NetworkCredential ( logonName , password ) ;
131134 connection . SessionOptions . RootDseCache = true ;
@@ -136,10 +139,11 @@ public LdapConnection CreateForAdlds(Uri ldapServer, string logonName, string pa
136139 {
137140 connection . SessionOptions . SecureSocketLayer = true ;
138141 }
142+ if ( connectionTimeout . HasValue )
143+ {
144+ connection . Timeout = connectionTimeout . Value ;
145+ }
139146
140- _logger . Debug ( "Start bind to {Scheme}://{Server} as '{User}'" ,
141- isLdaps ? "LDAPS" : "LDAP" , ldapServer . Authority , logonName ) ;
142- connection . Bind ( ) ;
143147 return connection ;
144148 }
145149 }
0 commit comments