【问题标题】:Not able to Set Password and Enable Account using C# and admin User无法使用 C# 和管理员用户设置密码和启用帐户
【发布时间】: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


【解决方案1】:

也许这只是您的问题中的一个错误,但是您在第一个示例中显示的代码无法编译,因为密码不在引号中。应该是:

directoryEntry.Invoke("SetPassword", new object[] {"myPass@x6712"});

该代码调用IADsUser.SetPassword'Remarks' in the documentation 指向它工作的一些先决条件,即它必须是安全连接。因此,它可能无法建立安全连接。它通常会尝试使用 Kerberos 来执行此操作,因此那里可能出现问题。

您可以通过将其指向端口 636 (new DirectoryEntry("LDAP://example.com:636/CN=whatever,DC=example,DC=com")) 尝试通过 LDAPS(基于 SSL 的 LDAP)进行专门连接,但这需要您信任所提供的证书。有时它是一个自签名证书,因此您需要将该证书添加到运行它的任何计算机上的受信任证书中。

或者,您运行它的帐户没有该帐户的“重置密码”权限。

为了启用,userAccountControl 属性是一个位标志,因此您不想将其设置为2,主要是因为2(或更准确地说,是第二位 ) 表示它已被禁用。所以你想取消设置第二位。你可以这样做:

directoryEntry.Properties["userAcountControl"].Value =
    (int) directoryEntry.Properties["userAcountControl"].Value & ~2;

大多数情况下会产生512 (NORMAL_ACCOUNT) 的值,但不一定。该帐户可能设置了您不想无意中取消设置的其他位。

您还需要致电.CommitChanges() 以使对userAcountControl 的更改生效:

directoryEntry.CommitChanges();

【讨论】:

  • 我都接受了“安全连接!这有何影响?
  • 对不起,我不明白你在说什么。你能改写一下吗?
  • 尝试设置密码时没有安全通信!正如你提到的,我在做剩下的事情。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-01
  • 2018-10-23
  • 2016-05-09
相关资源
最近更新 更多