c# - 为什么 User.Invoke ("SetPassword") 使用我计算机的当前凭据以及我如何指定它们?

标签 c# active-directory

我能够在远程 ActiveDirectory 服务器中创建新用户,我正在使用该服务器上的管理员帐户进行创建。我就是这样做的:

String connection = "LDAP://serveraddress/OU=NewUsers,OU=People,DC=mydomain,DC=local";
usersDirectoryEntry = new DirectoryEntry(connection, "adminUserName","adminPass");
DirectoryEntry newUser = usersDirectoryEntry.Children.Add(userString, "user");
newUser.CommitChanges();

但是,在我尝试使用以下方法设置密码后立即:

UpdatePassword(newUser, "userPasswordMeetsRequirements");

在哪里

private static void UpdatePassword(DirectoryEntry User, String password)
{
    User.Properties["pwdLastSet"].Value = -1;
    User.Invoke("SetPassword", new object[] { password.Trim() });
    User.Properties["userAccountControl"].Value = 0x0200 | 0x10000;
    User.CommitChanges();
}

其中 User 的类型为 DirectoryEntry ( https://msdn.microsoft.com/en-us/library/system.directoryservices.directoryentry(v=vs.110).aspx )

在 User.Invoke 行我得到一个 System.Reflection.TargetInvocationException --> System.UnauthorizedAccessException: Access is denied.

该管理员用户确实应该设置密码权限。查看 AD 日志后,我发现在 User.Invoke 行,我的程序正在尝试使用我计算机(位于不同域)上的当前凭据连接到 AD 服务器。

为什么会发生这种情况,我如何才能强制它仅使用用于创建用户的管理员帐户?

最佳答案

您是否将 newUser 对象传递给 UpdatePassword?我假设它使用相同的凭据。

但如果没有,您可以像这样创建一个新对象:

var user = new DirectoryEntry(newUser.Path, "adminUserName","adminPass");
UpdatePassword(user, somepassword);

关于c# - 为什么 User.Invoke ("SetPassword") 使用我计算机的当前凭据以及我如何指定它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34821141/

相关文章:

c# - 来自 NetworkCredentials 的用户权限掩码

c# - 将 Cookie 对象转换为字符串格式并返回

javascript - 更改 asp :TextBox css class depending on value using javascript

.net - 如何使用反射通过属性标签名称设置属性值?

php - 从浏览器获取用户身份

c# - 密码不应包含用户的帐户名或用户全名中超过两个连续字符的部分

c# - 方法声明中的变量 = null?这是什么意思,如何调用此方法?

powershell - 通过 PowerShell 更新 Active Directory "mail"属性

c# - 获取本地组的成员

active-directory - 导入模块 : The specified module 'activedirectory' was not loaded because no valid module file was found in any module directory