C# 服务器2019上调用的目标已引发DirectoryEntry CommitChanges返回异常

C# 服务器2019上调用的目标已引发DirectoryEntry CommitChanges返回异常,c#,asp.net,windows,directory,active-directory,C#,Asp.net,Windows,Directory,Active Directory,我尝试为Active Directory创建重置密码和用户创建函数。在我的电脑上,下面的代码可以正常工作,没有任何错误。但当我发布到服务器时,收到错误:调用的目标抛出了异常 ADResult hasil = new ADResult(); DirectoryEntry de = new DirectoryEntry(_path, _adminID, _adminPassword, AuthenticationTypes.Secure); DirectorySearcher ds = new D

我尝试为Active Directory创建重置密码和用户创建函数。在我的电脑上,下面的代码可以正常工作,没有任何错误。但当我发布到服务器时,收到错误:调用的目标抛出了异常

ADResult hasil = new ADResult();
DirectoryEntry de = new DirectoryEntry(_path, _adminID, _adminPassword, AuthenticationTypes.Secure);

DirectorySearcher ds = new DirectorySearcher(de);
string query = string.Format("(&(objectCategory=person)(sAMAccountName={0}))", user.userID);

ds.Filter = query;
ds.Sort.PropertyName = "CN";
ds.SearchScope = SearchScope.Subtree;
ds.CacheResults = false;

try
{
 SearchResult sr = ds.FindOne();
 if (sr == null)
 {
  hasil.errorCode = -1;
  hasil.result = "User name not found in this domain.";
 }
 else
 {
  DirectoryEntry userCredentials = sr.GetDirectoryEntry();
  userCredentials.Invoke("SetPassword", new Object[] { user.password });
  userCredentials.CommitChanges();
  userCredentials.Close();
  hasil.errorCode = 0;
  hasil.result = "Password for " + user.userID + " changed successfully.";
 }
}
catch (Exception e)
{
 hasil.errorCode = -1;
 hasil.result = e.Message + "<br/>" + e.StackTrace + "<br/>" + e.Source;
}
return hasil;
ADResult hasil=新的ADResult();
DirectoryEntry de=new DirectoryEntry(_path,_adminID,_adminPassword,AuthenticationTypes.Secure);
DirectorySearcher ds=新的DirectorySearcher(de);
字符串查询=string.Format(&(objectCategory=person)(sAMAccountName={0})),user.userID);
ds.Filter=查询;
ds.Sort.PropertyName=“CN”;
ds.SearchScope=SearchScope.Subtree;
ds.CacheResults=false;
尝试
{
SearchResult sr=ds.FindOne();
if(sr==null)
{
hasil.errorCode=-1;
hasil.result=“在此域中找不到用户名。”;
}
其他的
{
DirectoryEntry userCredentials=sr.GetDirectoryEntry();
调用(“SetPassword”,新对象[]{user.password});
userCredentials.CommitChanges();
userCredentials.Close();
hasil.errorCode=0;
hasil.result=“已成功更改“+user.userID+”的密码。”;
}
}
捕获(例外e)
{
hasil.errorCode=-1;
hasil.result=e.Message+“
”+e.StackTrace+“
”+e.Source; } 返回哈西尔;

服务器端是否有我遗漏的配置/设置?

这不是确切的错误消息,必须包装真正的错误。您可以在您认为可能是罪魁祸首的行之后写入记录器或事件日志。如果可以找到详细的堆栈跟踪,您可以检查该服务器上的事件日志。您可以检查用户是否具有该服务器在AD中查找的管理员权限。

我使用

用户主体

而不是

目录条目

而且它工作得很好。 我使用以下代码:

PrincipalContext PrincipalContext4 = new PrincipalContext(ContextType.Domain, "full_domain_name.com", "OU=User_OU,DC=domain_name,DC=co,DC=id", _adminID, _adminPassword);
UserPrincipal UserPrincipal1 = new UserPrincipal(PrincipalContext4, user.userID, user.password, true);

//User Logon Name
UserPrincipal1.UserPrincipalName = user.userID;
UserPrincipal1.Name = user.firstName + " " + user.lastName;
UserPrincipal1.GivenName = user.firstName;
UserPrincipal1.Surname = user.lastName;
UserPrincipal1.DisplayName = user.firstName + " " + user.lastName;
UserPrincipal1.Enabled = true;
UserPrincipal1.Save();

我仍然不知道为什么我使用的DirectoryEntry在windows server 2019上不起作用

错误总是在提交更改时停止:调用的目标引发了异常。位于System.DirectoryServices.DirectoryEntry.Invoke(String methodName,Object[]args)\r\n位于TAM.ActiveDirectory.Controllers.UserController.ResetPassword(ADUser用户)。或者对于重置AD password,我无法调用“SetPassword”?它作为AD客户端在我的PC上工作,但当我上传到服务器时,我收到了该错误。我同意,您需要找到更好的错误消息。查看异常的属性。然后看看它的
InnerException
。继续,直到您看到没有
InnerException
的异常。或者只需对异常调用
ToString()
,它将显示所有内部异常及其所有堆栈跟踪。您的pc是否连接到与生产服务器相同的域?可能存在组策略密码限制(长度、复杂性)。要验证密码是否符合要求,您需要以管理员用户身份登录,在生产服务器上打开ADUC,并尝试将用户密码重置为相同的值