【问题标题】:Active Directory search throws Exception with invalid name being searchedActive Directory 搜索引发异常,搜索的名称无效
【发布时间】:2017-07-10 12:59:24
【问题描述】:

我正在使用 asp.NET MVC5 网站对 Active Directory 中的用户进行研究。当我进行无效搜索时(例如'"éééézztaaz'),ArgumentException 不断被抛出,但我不明白。这是我的搜索方法:

public List<ADProperties> SearchUserByName(string name)
{
    //ADProperties is a POCO to store values retrieved
    try
    {

        List<ADProperties> theListIWant = new List<ADProperties>();
        //createDirectoryEntry() is a method to establish a connection to Active Directory
        DirectoryEntry ldapConnection = createDirectoryEntry();
        DirectorySearcher search = new DirectorySearcher(ldapConnection);

        //Search filter to find users
        search.Filter = "(&(objectClass=user)(anr=" + name + "))";

        ///Properties to load
        search.PropertiesToLoad.Add("objectSID");
        search.PropertiesToLoad.Add("displayName");
        search.PropertiesToLoad.Add("distinguishedName");
        resultCollection = search.FindAll();

        //ArgumentException at if statement
        //I put this to AVOID exceptions, then in my controller, if value is null 
        //I return a different view
        if (resultCollection==null ||resultCollection.Count==0)
        {
            return null;
        }

     }
     else
     { //Do stuff and return
          return theListIWant;
    }catch(ActiveDirectoryOperationException e)
    {
        Console.WriteLine("Active Directory Operation Exception caught: " + e.ToString());
    }
    return null;
}

确切的例外是:

搜索过滤器 (&(objectClass=user)(anr=)) 无效

(从法语翻译)

所以我不明白。我添加了条件以避免抛出异常,但显然它没有帮助。

【问题讨论】:

    标签: c# asp.net-mvc active-directory


    【解决方案1】:

    我建议改变:

    if (resultCollection==null ||resultCollection.Count==0)
    {
        return null;
    }
    

    到:

    try
    {
        if (resultCollection == null || resultCollection.Count == 0)
        {
            return null;
        }
    }
    catch (ArgumentException)
    {
        return null;
    }
    

    这将确保如果ArgumentException 被抛出,它将被视为resultCollection 为空。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多