Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
450 views
in Technique[技术] by (71.8m points)

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

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


question from:https://stackoverflow.com/questions/65936177/access-is-denied-exception-from-hresult-0x80070005-e-accessdenied-when-res

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...