2015-08-21 137 views
0

我是使用C#的新手,这是我第二次将它与活动目录结合使用。我不断收到错误:对象引用未设置为对象的实例。以下是我的代码。我知道我的空引用是在var result = searcher.FindOne();行我不确定我需要做什么来解决这个问题。C#搜索Active Directory错误

static void Main(string[] args) 
    { 
     List<string> userList = new List<string>(); 

     try 
     { 


      string[] newUsers = { List of users is here ex: [email protected], [email protected], ... }; 

      PrincipalContext AD = new PrincipalContext(ContextType.Domain, "xyz.com"); 
      UserPrincipal u = new UserPrincipal(AD); 
      PrincipalSearcher search = new PrincipalSearcher(u); 
      DirectorySearcher searcher = new DirectorySearcher(); 

      foreach (string x in newUsers) 
      { 
       searcher.Filter = string.Format("(&(objectCategory=person)(anr={0}))", x); 

       var result = searcher.FindOne(); 

       userList.Add(string.Format("{0} {1}", result.Properties["DisplayName"][0].ToString(), result.Properties["Company"][0].ToString())); 

       search.Dispose(); 
      } 

      foreach(string y in userList) 
      { 
       Console.WriteLine(y); 
      } 

      Console.ReadLine(); 
     } 

     catch (Exception e) 
     { 
      Console.WriteLine("Error: " + e.Message); 
     } 

     File.WriteAllLines(file location, userList); 
    } 
+0

可能重复[什么是一个NullReferenceException,如何解决呢?(http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i -fix-it) –

+0

你是否尝试过调试?这是找出发生异常的代码行的非常确定的方法。 – Jim

+1

您不检查search.FindOne()返回null,如果您的数组中的某个用户名被错误地指定,它可能会执行此操作。 –

回答

0

你的问题是,你是d eclaring PrincipalSearcherDirectorySearcher,但你只填充PrincipalSearcherUserPrincipal对象。

... 
UserPrincipal u = new UserPrincipal(AD); 
PrincipalSearcher search = new PrincipalSearcher(u); 
... 

但是,您的DirectorySearcher对象searcher为空。

var result = searcher.FindOne(); 

上面一行总是返回null

DirectorySearcher searcher = new DirectorySearcher(); 

foreach循环您正在使用DirectorySearcher对象不PrincipalSearcher寻找一个用户。您需要填写DirectorySearcher

DirectorySearcher searcher = new DirectorySearcher(/*need a DirectoryEntry*/); 

我建议你把UserPrincipal类的充分利用。看起来您要搜索Active Directory中的用户,并且您知道他们的UserPrincipal名称。的

using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "yourdomain.com")) 
{ 
    string [] newUsers; //need to declare list of new users 
    foreach (string user in newUsers) 
    { 
     using (UserPrincipal newUser = UserPrincipal.FindByIdentity(ctx, IdentityType.UserPrincipalName, user)) 
     { 
      if (newUser != null) 
      { 
      //do what you need to do 
      //newUser will contain all info on a particular user 
      } 
     } 
    } 
} 
0

正如一些评论者所指出的那样,你的代码不处理没有用户被DirectorySearcher.FindOne发现情况 - 而且,as noted in the MSDN documentationFindOne返回null如果没有用户发现:

If more than one entry is found during the search, only the first entry is returned. If no entries are found to match the search criteria, a null reference (Nothing in Visual Basic) is returned.

所以你'需要处理的情况下,你要找的用户不在:

foreach (string x in newUsers) 
    { 
     Console.WriteLine("looking for user {0}", x); 
     searcher.Filter = string.Format("(&(objectCategory=person)(anr={0}))", x); 
     var result = searcher.FindOne(); 
     if (result == null) 
     { 
      userList.Add(String.Format("user {0} not found!", x)); 
     } 
     else 
     { 
      userList.Add(string.Format("{0} {1}", result.Properties["DisplayName"][0].ToString(), result.Properties["Company"][0].ToString())); 
     } 
     search.Dispose(); 
    }