Active Directory 内のユーザーのパスワードを変更する次のコードがあります。
DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://0.0.0.0/CN=John Smith,OU=12345,OU=Project,DC=mysite,DC=local");
directoryEntry.AuthenticationType = AuthenticationTypes.Secure;
directoryEntry.Username = "adminusername";
directoryEntry.Password = "adminpassword";
directoryEntry.Invoke("SetPassword", new object[] { "newPassword" });
directoryEntry.Properties["LockOutTime"].Value = 0; //unlock account
このコードをアクティブなサーバーに直接実行しようとすると、これは完全に機能しますが、別のドメインにあるマシンで実行しようとすると、次のエラーが表示されます。
System.Reflection.TargetInvocationException: Exception has been thrown by the ta
rget of an invocation. ---> System.Runtime.InteropServices.COMException: The RPC
server is unavailable. (Exception from HRESULT: 0x800706BA)
同じ管理者ユーザーと他の資格情報を使用して、ユーザーの追加、ユーザーの削除、グループの追加、オブジェクトの名前の変更などを行うことができるため、これが唯一の制限です...ただし、パスワードは変更しません。
このコードで試したわけではありませんが、うまくいきません:
public bool SetPassword(string userName, string newPassword, Domain.ActiveDirectory.Credentials credentials)
{
try
{
using (var pc = new PrincipalContext(ContextType.Domain, credentials.ServerName, credentials.OrganizationalUnitsDn + "," + credentials.DomainControllerName))
{
using (var user = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, userName))
{
if (user == null)
{
return false;
}
user.SetPassword(newPassword);
return true;
}
}
}
catch (Exception e)
{
return false;
}
}
誰にもアイデアがありますか?
ありがとうございました。