System.DirectoryServices.AccountManagement PrincipalContext模



在Sharepoint(或任何ASP。. NET web应用程序)我想有一个功能来创建AD用户。我正在使用System.DirectoryServices.AccountManagement来完成这个任务,但是我遇到了麻烦。下面是我的代码:

using (var pc = new PrincipalContext(ContextType.Domain,"DOMAIN","administrator","password"))
{
    if (pc.ValidateCredentials("administrator", "password"))
    {
        UserPrincipal up = new UserPrincipal(pc, username, password, true);
        up.Save();
    }
}

用户被创建,但被禁用。我知道我的administrator:password对是正确的,因为"if"语句返回true。也在创建过程中,我收到Exception:

Exception has been thrown by the target of an invocation.

我检查了PrincipalContext对象,它与"管理员"帐户连接到域控制器。这个错误和up.Save()函数抛出异常的原因是什么?

您可以尝试以下操作吗:

using (var pc = new PrincipalContext(ContextType.Domain,"DOMAIN","administrator","password"))
            {
                using (var up = new UserPrincipal(pc))
                {
                    up.SamAccountName = textboxUsername.Text; // Username
                    up.SetPassword(textboxPassword.Text); // Password
                    up.Enabled = true;
                    up.ExpirePasswordNow();
                    up.Save();
                }
            }

这至少应该确保用户已创建并启用。

相关内容

  • 没有找到相关文章

最新更新