Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) when resetting password for some users

800 Views Asked by At

I'm getting above error when trying to reset password of some users.
This error does not happen for all users but only for some users.

Code snippet

        PrincipalContext principalContext = null;
        try {
            principalContext = new PrincipalContext(ContextType.Domain, DomainName, ServiceAC, ServicePWD);
        } catch (Exception e) {
            **code omitted**
        }

        UserPrincipal usr = null;

        try{
           
            usr = UserPrincipal.FindByIdentity(principalContext, user.userName);

        } catch(Exception e){
            **code omitted**
        }

    

        //user is available in active directory change password
        var newPassword = requestedPassword;
        usr.SetPassword(newPassword); // **This line throws error**

        usr.Enabled = true;


        try {
            usr.Save();
        } catch (Exception e) {
            **code omitted**
        }

When setPassword is called, it throws and error below.

Stack Trace

  Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
  at System.DirectoryServices.DirectoryEntry.Invoke(String methodName, Object[] args)
  at System.DirectoryServices.AccountManagement.SDSUtils.SetPassword(DirectoryEntry de, String 
  newPassword)
  at System.DirectoryServices.AccountManagement.ADStoreCtx.SetPassword(AuthenticablePrincipal 
  p, String newPassword)
  at System.DirectoryServices.AccountManagement.PasswordInfo.SetPassword(String newPassword)
  at System.DirectoryServices.AccountManagement.AuthenticablePrincipal.SetPassword(String 
  newPassword)```


0

There are 0 best solutions below