2015-08-21 154 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(); 
    }