如何使用C#在AD中更改密码? [英] How to change password in AD using C#?
问题描述
您好
我想使用C#在AD中更改用户的密码。找到用户(我的计算机)但在尝试更改密码时抛出异常(密码策略) - 我们选择了符合密码策略要求的密码。在我的朋友计算机上找不到用户。我们不知道什么是错的 - 用户存在于两台计算机上(lokal用户)。
我们在互联网上搜索了很长时间但没找到任何解决我们的问题。
我们感谢您的帮助。谢谢。
我尝试了什么:
我试过以下代码:
Hello
I would like to change a user's passwort in AD using C#. The user has been found (my computer) but throws exception (password policy) while trying to change the password - we chose a password that meets the password policy requirements. On my friends computer the user couldn't be found. We don't know what's wrong - the user exists on both computers (lokal user).
We searched for a long time on the internet but didn't find any solution for our problem.
We appreciate every help. Thank you.
What I have tried:
I tried following code:
String domain = configurationProvider.Configuration.DomainName;
ContextType contextType = ContextType.Machine;
public int newPassword(string username, string oldPassword, string newPassword)
using (PrincipalContext pc = new PrincipalContext(contextType, domain))
{
using (UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(pc, username))
{
if (userPrincipal != null)
{
try { userPrincipal.ChangePassword(oldPassword, newPassword); }
catch (PasswordException pe) { return null; }
}
}
}
}
推荐答案
搞定了!
这只是一个缺少的注册表项 - RegisteredOrganization和RegisteredOwner!
Got it!
It was only a missing registry entry - RegisteredOrganization and RegisteredOwner!
这篇关于如何使用C#在AD中更改密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!