有5个或6个类似的线程,但没有一个是这个问题的解决方案。似乎当我尝试为新用户帐户创建新的主要用户时...它似乎在服务器上搜索该不存在的帐户。我发布了我的所有代码,在线发现的各种版本的解决方案似乎都没有被注释掉(即最后一部分,只有未注释的部分是让我在这个过程中最远的代码并生成这个错误)......我已经在这3天了:
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();
}
}
代码在这一行失败(就像它试图找到用户我试图创建???):
UserPrincipal NewUserPrincipal = new UserPrincipal(context, user_name, password, true);
完整错误消息:
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