【问题标题】:Unable to read Mobile # field from Active Directory无法从 Active Directory 中读取 Mobile # 字段
【发布时间】:2019-03-21 06:39:21
【问题描述】:

我正在尝试连接到Active Directory (2003) 以更新出现在OutLook 通讯簿中的Mobile # 字段,如下图所示。

我可以使用下面的代码读取大部分字段,但找不到 otherTelephonemobileotherMobile 字段。是什么原因?

static void Main(string[] args)
    {
        Console.Write("Enter user      : ");
        String username = Console.ReadLine();

        try
        {
            DirectoryEntry myLdapConnection = createDirectoryEntry();

            DirectorySearcher search = new DirectorySearcher(myLdapConnection,);
            search.Filter = "(sAMAccountName=" + username + ")";
            search.PropertiesToLoad.Add("title");
            search.PropertiesToLoad.Add("street");
            search.PropertiesToLoad.Add("department");
            search.PropertiesToLoad.Add("mail");
            search.PropertiesToLoad.Add("manager");
            search.PropertiesToLoad.Add("telephoneNumber");
            search.PropertiesToLoad.Add("otherTelephone");
            search.PropertiesToLoad.Add("mobile");
            search.PropertiesToLoad.Add("otherMobile");


            SearchResult result = search.FindOne();

            if (result != null)
            {
                DirectoryEntry entryToUpdate = result.GetDirectoryEntry();
                Console.WriteLine("Current title   : " + entryToUpdate.Properties["title"][0].ToString());

                //Console.Write("\n\nEnter new title : ");
                //String newTitle = Console.ReadLine();
                //entryToUpdate.Properties["title"].Value = newTitle;
                //entryToUpdate.CommitChanges();
                //Console.WriteLine("\n\n...new title saved");
                Console.ReadLine();
            }

            else Console.WriteLine("User not found!");
        }

        catch (Exception e)
        {
            Console.WriteLine("Exception caught:\n\n" + e.ToString());
        }
    }

    static DirectoryEntry createDirectoryEntry()
    {
        // create and return new LDAP connection with desired settings  
        DirectoryEntry ldapConnection = new DirectoryEntry("abc.ca");
        ldapConnection.Path = "LDAP://OU=staffusers,DC=leeds-art,DC=ac,DC=uk";
        ldapConnection.AuthenticationType = AuthenticationTypes.Secure;
        return ldapConnection;
    }

【问题讨论】:

  • 您能否解释一下“未找到”的含义,因为这些属性是标准 AD 属性。它们可能没有价值。您发布的屏幕截图对这些字段中的任何一个都没有价值。
  • @Ashigore 我确实为某些用户提供了这些属性的值,但故意截取了我没有这些值的用户的屏幕截图。 result 变量的 Properties 属性不包含属性 mobileotherMobile、'otherTelephone. Some other standard attributes for some employees also don't have values, but they do appear in the Properties with null` 或 empty 值。

标签: c# active-directory ldap directoryentry


【解决方案1】:

我认为您的问题是某些(阅读最多的)属性默认情况下不会在对象上设置,它们仅在第一次设置值时才针对该对象存在。您需要更新代码以处理根本不存在的值,并假设这意味着它没有设置值。

您可以通过对特定用户运行查询来测试这一点,观察缺少mobile 属性,然后向该字段添加一个值,然后将其删除。然后,您应该会看到该属性从那时起包含在该用户的列表中。

【讨论】:

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