C#搜索Active Directory错误

时间:2015-08-21 18:18:33

标签: c# active-directory

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

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

        try
        {


            string[] newUsers = { List of users is here ex: jsmith@xyz.com, bsmith@xyz.com, ... };

            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);
    }

2 个答案:

答案 0 :(得分:0)

正如一些评论者所指出的那样,您的代码未处理DirectorySearcher.FindOne未找到用户的情况 - 如果没有用户,则as noted in the MSDN documentationFindOne会返回null找到了:

  

如果在搜索过程中找到多个条目,则仅返回第一个条目。如果未找到与搜索条件匹配的条目,则返回空引用(在Visual Basic中为Nothing)。

因此,您需要处理您所寻找的用户不在的情况:

    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();
    }

答案 1 :(得分:0)

您的问题是,您正在宣布PrincipalSearcherDirectorySearcher,但您只是使用PrincipalSearcher对象填充UserPrincipal

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

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

DirectorySearcher searcher = new DirectorySearcher();

foreach循环中,您使用DirectorySearcher对象而不是PrincipalSearcher搜索一位用户:

var result = searcher.FindOne();

以上行始终会返回null。您需要填充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
          }
       }
   }
}
相关问题