C#MVC新Active Directory用户System.DirectoryServices.AccountManagement.PrincipalOperationException:服务器上没有此类对象

C#MVC新Active Directory用户System.DirectoryServices.AccountManagement.PrincipalOperationException:服务器上没有此类对象,c#,asp.net-mvc,active-directory,C#,Asp.net Mvc,Active Directory,有5个或6个类似的线程,但都不是这个问题的解决方案。似乎当我尝试为新用户帐户创建新的主要用户时。。。它似乎正在搜索服务器上不存在的帐户。我发布了我所有的代码,在网上找到的各种版本的解决方案似乎都没有被注释掉(即,最后一部分,也是唯一未注释的部分,是让我在过程中走得最远并产生此错误的代码)。。。我已经做了三天了: public string AddADUser(string user_name, string password, string first_name, string last_nam

有5个或6个类似的线程,但都不是这个问题的解决方案。似乎当我尝试为新用户帐户创建新的主要用户时。。。它似乎正在搜索服务器上不存在的帐户。我发布了我所有的代码,在网上找到的各种版本的解决方案似乎都没有被注释掉(即,最后一部分,也是唯一未注释的部分,是让我在过程中走得最远并产生此错误的代码)。。。我已经做了三天了:

public string AddADUser(string user_name, string password, string first_name, string last_name, string company_name) {

    /* string admin_userName = "SNEAKY/Administrator";
     string admin_password = "Password!";
     PrincipalContext ouContex = new PrincipalContext(ContextType.Domain, "sneaky.com", admin_userName, admin_password, ContextType.Domain, "OU=Users,DC=Sneaky,DC=com");

     for (int i = 0; i < 3; i++) {
         try {
             UserPrincipal up = new UserPrincipal(ouContex);
             up.SamAccountName = "Administrator" + i;
             up.SetPassword("Password!");
             up.Enabled = true;
             up.ExpirePasswordNow();
             up.Save();
             return "success";
         } catch (Exception ex) {
             return "FAILED:" + ex;
         }
     }

     return "success";*/
    /*string oGUID = string.Empty;
    string ldapPath = "LDAP://OU=USERS,DC=Sneaky,DC=com";
    try {
        string connectionPrefix = "LDAP://" + ldapPath;
        DirectoryEntry dirEntry = new DirectoryEntry(connectionPrefix);
        DirectoryEntry newUser = dirEntry.Children.Add
            ("CN=" + user_name, "user");
        newUser.Properties["samAccountName"].Value = user_name;
        newUser.CommitChanges();
        oGUID = newUser.Guid.ToString();

        newUser.Invoke("SetPassword", new object[] { password });
        newUser.CommitChanges();
        dirEntry.Close();
        newUser.Close();
    } catch (System.DirectoryServices.DirectoryServicesCOMException E) {
        //DoSomethingwith --> E.Message.ToString();

    }
    return oGUID;*/

    /*try {
        PrincipalContext ctx = new PrincipalContext(
                                 ContextType.Domain,
                                 "sneaky",
                                 "CN=Users,DC=sneaky,DC=com",
                                 "Administrator",
                                 "Password!");

        UserPrincipal usr = new UserPrincipal(ctx);

        usr.Name = "Jim Daly";
        usr.Description = "This is the user account for Jim Daly";
        usr.EmailAddress = "jimdaly@fabrikam.com";
        usr.SetPassword("securelyStoredPassword");
        usr.Save();

        usr.Dispose();
        ctx.Dispose();
        return "User Saved Sucessfully";
    } catch (Exception ex) {
        return "Error saving user: \n" + ex.ToString();
    }*/



    try {
        List<ADUser> ADUsers = new List<ADUser>();
        string admin_userName = "sneakyguy";
        string admin_password = "Password!";
        string domain = "sneaky";
        var context = new PrincipalContext(ContextType.Domain, domain, "OU=Users,DC=Sneaky,DC=com", admin_userName, admin_password);


        UserPrincipal NewUserPrincipal = new UserPrincipal(context, user_name, password, true);

        NewUserPrincipal.UserPrincipalName = user_name;

        NewUserPrincipal.ExpirePasswordNow();
        //NewUserPrincipal.SamAccountName = user_name;
        // company NewUserPrincipal.GetUnderlyingObject.

        //NewUserPrincipal.GivenName = first_name;
        //NewUserPrincipal.Surname = last_name;
        //NewUserPrincipal.DisplayName = user_name;
        //NewUserPrincipal.Enabled = true;

        NewUserPrincipal.Save();
        return "User Saved Sucessfully";
    } catch (Exception ex) {
        return "Error saving user: \n" + ex.ToString();
    }
}
完整错误消息:

System.DirectoryServices.AccountManagement.PrincipalOperationException: There is no such object on the server. ---> System.DirectoryServices.DirectoryServicesCOMException: There is no such object on the server. at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) at System.DirectoryServices.DirectoryEntry.Bind() at System.DirectoryServices.DirectoryEntry.get_SchemaEntry() at System.DirectoryServices.AccountManagement.ADStoreCtx.IsContainer(DirectoryEntry de) at System.DirectoryServices.AccountManagement.ADStoreCtx..ctor(DirectoryEntry ctxBase, Boolean ownCtxBase, String username, String password, ContextOptions options) at System.DirectoryServices.AccountManagement.PrincipalContext.CreateContextFromDirectoryEntry(DirectoryEntry entry) at System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInit() --- End of inner exception stack trace --- at System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInit() at System.DirectoryServices.AccountManagement.PrincipalContext.DoDomainInit() at System.DirectoryServices.AccountManagement.PrincipalContext.Initialize() at System.DirectoryServices.AccountManagement.PrincipalContext.ContextForType(Type t) at System.DirectoryServices.AccountManagement.Principal.GetStoreCtxToUse() at System.DirectoryServices.AccountManagement.Principal.set_SamAccountName(String value) at System.DirectoryServices.AccountManagement.UserPrincipal..ctor(PrincipalContext context, String samAccountName, String password, Boolean enabled) at SneakPM.Models.UserBAL.AddADUser(String user_name, String password, String first_name, String last_name, String company_name) in C:\Users\sneakyguy\Desktop\SneakyPM\SneakPM\Models\UserBAL.cs:line 162

您能显示哪一行抛出PrincipalOperationException吗?似乎应该检查
PrincipalContext
用法。更新信息。我觉得我已经尝试了每一个论点的每一个组合。
System.DirectoryServices.AccountManagement.PrincipalOperationException: There is no such object on the server. ---> System.DirectoryServices.DirectoryServicesCOMException: There is no such object on the server. at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) at System.DirectoryServices.DirectoryEntry.Bind() at System.DirectoryServices.DirectoryEntry.get_SchemaEntry() at System.DirectoryServices.AccountManagement.ADStoreCtx.IsContainer(DirectoryEntry de) at System.DirectoryServices.AccountManagement.ADStoreCtx..ctor(DirectoryEntry ctxBase, Boolean ownCtxBase, String username, String password, ContextOptions options) at System.DirectoryServices.AccountManagement.PrincipalContext.CreateContextFromDirectoryEntry(DirectoryEntry entry) at System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInit() --- End of inner exception stack trace --- at System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInit() at System.DirectoryServices.AccountManagement.PrincipalContext.DoDomainInit() at System.DirectoryServices.AccountManagement.PrincipalContext.Initialize() at System.DirectoryServices.AccountManagement.PrincipalContext.ContextForType(Type t) at System.DirectoryServices.AccountManagement.Principal.GetStoreCtxToUse() at System.DirectoryServices.AccountManagement.Principal.set_SamAccountName(String value) at System.DirectoryServices.AccountManagement.UserPrincipal..ctor(PrincipalContext context, String samAccountName, String password, Boolean enabled) at SneakPM.Models.UserBAL.AddADUser(String user_name, String password, String first_name, String last_name, String company_name) in C:\Users\sneakyguy\Desktop\SneakyPM\SneakPM\Models\UserBAL.cs:line 162