首页 > 解决方案 > 无法从 Active Directory 中读取 Mobile # 字段

问题描述

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

在此处输入图像描述

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

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

标签: c#active-directoryldapdirectoryentry

解决方案


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

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


推荐阅读