Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set connection time out when establishing context - PrincipalContext

using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, Domain, UserName, Password))
            {
                UserPrincipal U = new UserPrincipal(ctx);
                U.GivenName = strFirstName;
                U.Surname = strLastName;
                U.EmailAddress = strEmail;

                PrincipalSearcher srch = new PrincipalSearcher(U);

                foreach (var principal in srch.FindAll())
                {
                    var p = (UserPrincipal)principal;
                    if (!User.Any(x => x.Email == p.EmailAddress))
                    {
                        MyUserDataset.UserRow User = User.NewUserRow();
                        User.FirstName = p.GivenName;
                        User.LastName = p.Surname;
                        User.UserName = p.SamAccountName;
                        User.Email = p.EmailAddress;
                        User.AddUserRow(User);
                    }
                }
                User.AcceptChanges();
            }

I'm using the PrincipalContext class above to establish a connection to the target directory and specify credentials for performing operations against the directory.

Does any one know how i can also specify the connection time out in the PrincipalContext Constructor?, i'm running into connection time out issues & i was wondering if i can control after how long the connection can time out.

like image 659
StackTrace Avatar asked Mar 05 '15 06:03

StackTrace


1 Answers

Well, I guess the answer is no unfortunately. I have dig into the source code of PrincipalContext, it used DirectoryEntry which used unsafe native method System.DirectoryServices.Interop.UnsafeNativeMethods.ADsOpenObject to open LDAP connection.

As per this blog How to specify TimeOut for ldap bind in .Net, there is no way to configure the timeout on ADsOpenObject. However, it also mentioned that if using LdapConnection directly, then it is possible to set the timeout. In that case, you may not be able to user PrincipalContext.

like image 111
bigbearzhu Avatar answered Oct 22 '22 05:10

bigbearzhu