如何使用C#更改Active Directory中的电话号码

时间:2012-11-19 20:06:53

标签: c# active-directory

我有这个代码,我可以在Active Directory中更改显示名称,密码等

UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(principalContext, userName);
userPrincipal.DisplayName = "Some NAME";
userPrincipal.SetPassword("NEW_PASSWORD");
userPrincipal.Save();

我查看了userPrincipal的属性,但找不到电话号码属性。我的问题是如何在代码中更改用户的电话号码。

谢谢

1 个答案:

答案 0 :(得分:5)

更正(对不起所有修改):

这就是我做的......

    public static void SetUserInfo(string userName)
    {
        var dsDirectoryEntry = new DirectoryEntry("LDAP://xxxx/DC=xx,DC=xxx", "ADusername", "ADpassword");

        var dsSearch = new DirectorySearcher(dsDirectoryEntry) { Filter = "(&(objectClass=user)(SAMAccountName=" + userName + "))" };

        var dsResults = dsSearch.FindOne();
        var myEntry = dsResults.GetDirectoryEntry();
        //myEntry.Properties[property].Value = value;
        myEntry.Properties["telephoneNumber"].Value = "222-222-2222";
        myEntry.CommitChanges();
    }