c#搜索活动目录错误

本文关键字:错误 活动 搜索 | 更新日期: 2023-09-27 18:09:13

我对使用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);
    }

c#搜索活动目录错误

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

...
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
          }
       }
   }
}

正如几个评论者所指出的,您的代码没有处理DirectorySearcher.FindOne没有找到用户的情况-并且,正如MSDN文档中所指出的,如果没有找到用户,FindOne返回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();
    }