C# AD user ChangePassword error 0x56 the specified network passw

Apr 5 2022 11:27 AM

I'm asking for help about a C# asp net program that change password of ad users.

It works well most of the time, but sometimes i have this case :

Exception de HRESULT : 0x80070056 The specified network password is not correct System.DirectoryServices.AccountManagement.ADStoreCtx.ChangePassword(AuthenticablePrincipal p, String oldPassword, String newPassword)

This is my code :

PrincipalContext ad = new PrincipalContext(ContextType.Domain, "adomaincontroller", "specificOU", "domainadmin", "password");

if (ad.ValidateCredentials(model.UserName, model.OldPassword))
{
    ModifyPassword("adomaincontroller", model.UserName, model.OldPassword, model.Password);
    return RedirectToAction("ChangePassword", "ChangePassword");
}

public void ModifyPassword(string domain, string userName, string oldPassword, string newPassword)
{
    try
    {
        using (var context = new PrincipalContext(ContextType.Domain, domain))
        using (var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, userName))
        {
            user.ChangePassword(oldPassword, newPassword);
        }
    }
    catch (Exception ex)
    {
            some stuf to log error here

    }
}

I can assure you the old password specified is correct. I check it with ValidateCredentials.

So in some case this error hapenned, but ... the user's password is changed anyway, so it works but the error apperead ... I don't undestand.

I saw some threads about this error, speaking about windows KB, but its aparently not my case.

Thanks for help


Answers (2)