DirectoryServices.AccountManagement.UserPrincipal.SetPassword("BLahBlahBlah")

1.8k views Asked by At

Can someone help me out with this? I have this code running fine on xp 32 bit, but on xp 64 bit "user.SetPassword" (not .Save()) throws an exception:

"Exception has been thrown by the target of an invocation."

{"Access Denied (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))"}

The code:

        UserPrincipal user = UserPrincipal.FindByIdentity
          (new PrincipalContext(ContextType.Domain, Domain, "MYADMINUSER", "MYADMINPASSWORD"), IdentityType.SamAccountName, Account);
        user.SetPassword("BlahBlahBlah");
        user.Save();

Both Clients are in the same domain, and the target in both cases is the same user. "MYADMINUSER" can log in to both clients, and modify the target user using ADUC.

Any ideas?

1

There are 1 answers

1
JamieSee On

This is most likely nothing to do with 32- or 64-bit. It's probably an issue with User Account Control preventing the operation. You can try right-clicking your app and choosing Run As administrator to check this.

See Step 6: Create and Embed an Application Manifest (UAC) for how to add a manifest to your application that can be used to make it request the necessary privilege elevation on its own.