【发布时间】:2019-10-11 16:53:42
【问题描述】:
使用 WPF & C#,我可以设置 Active Directory 中的所有属性,但不能执行以下操作:
1) 无法设置用户密码
2) 无法启用用户
但是,我可以手动执行相同的操作!
尝试的方法:
1.
DirectoryEntry directoryEntry=
directoryEntry.Invoke("SetPassword", new object[] {myPass@x6712}); // To set password
directoryEntry.Properties["userAcountControl"].Value=0x0200; //To Enable User
2.
DirectoryEntry uEntry = new DirectoryEntry(userDn);
uEntry.Invoke("SetPassword", new object[] { password });
uEntry.Properties["LockOutTime"].Value = 0; //unlock account
3.
using (var context = new PrincipalContext( ContextType.Domain ))
{
using (var user = UserPrincipal.FindByIdentity( context, IdentityType.SamAccountName, userName ))
{
user.SetPassword( "newpassword" );
// or
user.ChangePassword( "oldPassword", "newpassword" );
user.Save();
}
}
密码设置错误:目标调用抛出异常。
ENABLE USER 出错:访问被拒绝。
注意:我使用的是域管理员用户。
程序在上面的这些行中给出了异常。
请,建议!提前致谢!!
【问题讨论】:
-
作为一名资深的应用程序开发人员和一年多的会员,您会考虑发布堆栈跟踪吗?请visit 帮助中心并查看链接。
Nothing Helped! the program gives the exception in these above lines,这是什么异常,我没有看到。能否请您更新您的帖子以包含相关信息以便我们帮助您,否则我们无济于事,谢谢。 -
Exception has been thrown by the target invocation这个错误是因为invoke在这一行:directoryEntry.Invoke("SetPassword", new object[] {myPass@x6712});... 有了这种错误,我知道事实上你会有一个堆栈跟踪会告诉你我们更多地了解这个错误。另外,听起来当前用户没有权限这样做,您是否尝试过设置应用程序的执行级别?例如在应用程序清单文件中:<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />... -
是的!执行级别是“requireAdministrator”也尝试了“highestAvailable”
标签: c# active-directory