Asp.net System.DirectoryServices.AccountManagement PrincipalContext模拟以创建新用户

Asp.net System.DirectoryServices.AccountManagement PrincipalContext模拟以创建新用户,asp.net,sharepoint-2010,directoryservices,userprincipal,principalcontext,Asp.net,Sharepoint 2010,Directoryservices,Userprincipal,Principalcontext,在Sharepoint(或任何ASP.NET web应用程序)中,我希望具有创建广告用户的功能。我正在使用System.DirectoryServices.AccountManagement执行此任务,但遇到了麻烦。这是我的密码: using (var pc = new PrincipalContext(ContextType.Domain,"DOMAIN","administrator","password")) { if (pc.ValidateCredentials("admini

在Sharepoint(或任何ASP.NET web应用程序)中,我希望具有创建广告用户的功能。我正在使用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 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();
                }
            }

这至少应确保用户已创建并已启用。让我知道它是否有效。

这可能更适合Sharepoint堆栈。此问题适用于所有ASP.NET web应用程序。Sharepoint只是一个关键字此处的错误与您上面的错误相同还是有新的错误?