如何在Active Directory中更改用户的登录名

我想在Active Directory中更改用户的.NET应用程序登录。

我现在正以这种方式改变它:

DirectoryEntry userToUpdate = updatedUser.GetDirectoryEntry(); userToUpdate.Properties["sAMAccountName"].Value = user.NewLogin; userToUpdate.CommitChanges(); 

但它并不像我预期的那样有效。 当我在为此用户检入AD“Active Directory用户和计算机”条目时,然后在“帐户”选项卡上,我看到:
– “用户登录名”属性未更新
– “用户登录名(pre-Windows 2000)”属性已正确更新。

如何从C#代码正确更新AD中的登录名? 我应该在DirectoryEntry中设置什么属性,或者有另一种方法来更改登录名。

AD中有两个登录名:

 sAMAccountName = User logon name, (pre-windows 2000) Format/Usage: domain\user.name (note, your code will only populate user.name) userPrincipalName = User logon name Format/Usage: user.name@domain.local 

您需要更新两者。

尝试使用userPrincipalName而不是sAMAccountName

这是一个非常好的链接,你可以参考一定要检查页面底部,看看它的完整用法

通过C#.Net 3.5在Active Directory中的所有内容(使用System.DirectoryServices.AccountManagement)