Change Password – Exception has been thrown by the target of an invocation

You may receive the error “System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation” when you try to change an AD user password with System.DirectoryServices.DirectoryEntry in C#.NET with following code.

var userEntry = new DirectoryEntry("LDAP://<GUID=" + guid + ">");
    userEntry.Invoke("ChangePassword", new object[] { oldPassword, newPassword });
    userEntry.CommitChanges();

Solution

The error “Exception has been thrown by the target of an invocation” is a common error, it may occurs due to following cases:

1) The new password doesn’t meet the domain complexity requirements.

2) Minimum Password Age is > 0.

3) Enforce password history may configured.

4) Any of the passwords is incorrect.

5) The user property “UserCannotChangePassword” set as True (Account options: User cannot change password).

You might have received on of the below error message.

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
   --- End of inner exception stack trace ---
   at System.DirectoryServices.DirectoryEntry.Invoke(String methodName, Object[] args)
   at System.DirectoryServices.AccountManagement.SDSUtils.ChangePassword(DirectoryEntry de, String oldPassword, String newPassword)
   at System.DirectoryServices.AccountManagement.ADStoreCtx.ChangePassword(AuthenticablePrincipal p, String oldPassword, String newPassword)
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.DirectoryServices.DirectoryServicesCOMException (0x8007202F): A constraint violation occurred. (Exception from HRESULT: 0x8007202F)
Advertisement

Leave a Comment