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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…