C#搜索Active Directory错误

C#搜索Active Directory错误,c#,active-directory,C#,Active Directory,我对使用C#非常陌生,这是我第二次在active directory中使用它。我不断得到错误:对象引用未设置为对象的实例。下面是我的代码。我知道我的空引用在var result=searcher.FindOne()行中我不确定我需要做什么来修复此问题 static void Main(string[] args) { List<string> userList = new List<string>(); try {

我对使用C#非常陌生,这是我第二次在active directory中使用它。我不断得到错误:对象引用未设置为对象的实例。下面是我的代码。我知道我的空引用在
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);
    }
static void Main(字符串[]args)
{
List userList=新列表();
尝试
{
string[]newUsers={用户列表如下:jsmith@xyz.com, bsmith@xyz.com, ... };
PrincipalContext AD=新PrincipalContext(ContextType.Domain,“xyz.com”);
UserPrincipal u=新的UserPrincipal(AD);
PrincipalSearcher=新PrincipalSearcher(u);
DirectorySearcher search=新的DirectorySearcher();
foreach(newUsers中的字符串x)
{
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(userList中的字符串y)
{
控制台写入线(y);
}
Console.ReadLine();
}
捕获(例外e)
{
Console.WriteLine(“错误:+e.Message”);
}
writeAllines(文件位置、用户列表);
}

正如一些评论者所指出的,您的代码没有处理DirectorySearcher找不到用户的情况。FindOne
-如果找不到用户,
FindOne
返回
null

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

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

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

正如一些评论者所指出的,您的代码没有处理DirectorySearcher找不到用户的情况。FindOne-如果找不到用户,
FindOne
返回
null

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

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

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

您的问题是您正在声明
PrincipalSearcher
DirectorySearcher
,但您只使用
userprincipalsearcher
对象填充
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
类。似乎您想在
activedirectory
中搜索用户,并且您知道他们的
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
          }
       }
   }
}

您的问题是您正在声明
PrincipalSearcher
DirectorySearcher
,但您只使用
userprincipalsearcher
对象填充
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
类。似乎您想在
activedirectory
中搜索用户,并且您知道他们的
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
          }
       }
   }
}

可能的副本您是否尝试过调试?这是确定发生异常的代码行的一种非常可靠的方法。您没有检查search.FindOne()是否返回null,如果数组中的某个用户名被错误指定,它可能会返回null。我知道这是我的var result=searcher.FindOne();这是空的,我只是不知道如何解决这个问题。可能是重复的,您是否尝试过调试?这是确定发生异常的代码行的一种非常可靠的方法。您没有检查search.FindOne()是否返回null,如果数组中的某个用户名被错误指定,它可能会返回null。我知道这是我的var result=searcher.FindOne();这是空的,我只是不知道如何解决这个问题。