Sharepoint 2010没有绑定到Active Directory

本文关键字:Active Directory 绑定 2010 Sharepoint | 更新日期: 2023-09-27 18:05:37

我试图从活动目录中获取用户的管理器名称,我创建了一个控制台程序来测试它,一切都很好,当我在Sharepoint 2010程序中尝试代码时,它只是给了我一个例外"发生了操作错误"。

在进一步调试后,我发现这是一个与我的principalcontext有关的错误,它抛出了异常"为了执行此操作,必须在连接上完成成功的绑定"。代码如下(注意:try catch中的代码与我在控制台应用程序中的代码完全相同):

   public override void ItemAdding(SPItemEventProperties properties)
   {
       base.ItemAdding(properties);
       try 
       {
               // set up domain context
               PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
               // find a user
               UserPrincipal user = UserPrincipal.FindByIdentity(ctx, Environment.UserName);
               string samAccountName = "";

               if (user != null)
               {
                   // do something here....     
                   samAccountName = user.SamAccountName;
               }

               //Get the manager name from the active directory
               var domain = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName;
               using (DirectoryEntry dir = new DirectoryEntry("LDAP://" + domain))
               {
                   using (DirectorySearcher ds = new DirectorySearcher(dir, "samAccountName=" + samAccountName))
                   {
                       SearchResult result = ds.FindOne();
                       string managerName = result.Properties["manager"][0].ToString();
                   }
               }

       }
       catch(Exception ex)
       {
           var message = ex.Message;
       }
   }

Sharepoint 2010没有绑定到Active Directory

尝试更改:

PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "YOURDOMAINNAME");