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

0

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)```


c#
asp.net
azure-active-directory
active-directory-group
asked on Stack Overflow Jan 28, 2021 by Local Ghost

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0