Welcome to MSDN Blogs Sign in | Join | Help

Decrypt my World

Cryptography, Security, Debugging and more!
ChangePassword method may fail with TargetInvocationException (.NET)

Hi all, welcome back,

When working with System.DirectoryServices.DirectoryEntry in .NET, we may change the password of the user with a code like the following (C#):

user.Invoke("ChangePassword", new object[] { oldPassword, newPassword }

But invoking ChangePassword may fail with the following System.Reflection.TargetInvocationException:

"Exception has been thrown by the target of an invocation"

This error is not very descriptive, I know. I've seen several causes for this error in the past:

1) Any of the passwords is incorrect.

2) The new password doesn't meet the domain complexity requirements.

3) Minimum Password Age is > 0.

4) WinNT provider is used instead of LDAP.

 

By using InnerException.ToString() from the TargetInvocationException we may get a more descriptive error message. We could even see the HResult value associated to the exception. But this property is protected, so we could try to parse it from the error message string, with a code like this:

string errorMessage;
Int32 errorCode = 0;
try
{
    ...
}
catch (TargetInvocationException e)
{
    errorMessage = e.InnerException.ToString();
    try
    {
        string HResult = errorMessage.Substring(errorMessage.IndexOf("0x") + 2, 8);
        errorCode = Int32.Parse(HResult, System.Globalization.NumberStyles.HexNumber);
    }
    catch
    {
        errorCode = -1;
    }
}

 

I hope this helps.

Cheers,

 

Alex (Alejandro Campos Magencio)

Posted: Tuesday, April 29, 2008 9:03 AM by alejacma

Comments

Fran said:

Thanks Alex for these cool scripts, we keep tuned

Regards, ele,

fran

# April 29, 2008 11:18 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker