【问题标题】:How to set connection time out when establishing context - PrincipalContext建立上下文时如何设置连接超时 - PrincipalContext
【发布时间】:2015-03-05 06:21:09
【问题描述】:
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();
            }

我正在使用上面的 PrincipalContext 类来建立到目标目录的连接并指定凭据以对目录执行操作。

有人知道我如何在 PrincipalContext 构造函数中指定连接超时吗?我遇到了连接超时问题,我想知道我是否可以控制连接超时多长时间。

【问题讨论】:

    标签: c# c#-4.0 directoryservices adsi principalcontext


    【解决方案1】:

    好吧,不幸的是,我想答案是否定的。我已经深入研究了 PrincipalContext 的源代码,它使用了 DirectoryEntry,它使用了不安全的本机方法 System.DirectoryServices.Interop.UnsafeNativeMethods.ADsOpenObject 来打开 LDAP 连接。

    根据此博客:http://blogs.msdn.com/b/dsadsi/archive/2012/06/06/how-to-specify-timeout-for-ldap-bind-in-net.aspx,无法在 ADsOpenObject 上配置超时。但是,它也提到如果直接使用 LdapConnection,那么可以设置超时。在这种情况下,您可能无法使用 PrincipalContext。

    【讨论】:

      猜你喜欢
      • 2011-05-10
      • 1970-01-01
      • 2017-04-17
      • 2015-07-13
      • 2013-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多