Sharepoint 2010未绑定到Active Directory

时间:2013-03-25 14:45:15

标签: c# .net binding sharepoint-2010 active-directory

我正在尝试从活动目录中获取用户的管理员名称,我创建了一个控制台程序来测试它并且一切正常,当我在我的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;

       }
   }

1 个答案:

答案 0 :(得分:0)

尝试更改:

PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

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