Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

I would like to change a user's passwort in AD using C#. The user has been found (my computer) but throws exception (password policy) while trying to change the password - we chose a password that meets the password policy requirements. On my friends computer the user couldn't be found. We don't know what's wrong - the user exists on both computers (lokal user).

We searched for a long time on the internet but didn't find any solution for our problem.

We appreciate every help. Thank you.

What I have tried:

I tried following code:
C#
String domain = configurationProvider.Configuration.DomainName;
ContextType contextType = ContextType.Machine;
        
public int newPassword(string username, string oldPassword, string newPassword)
  using (PrincipalContext pc = new PrincipalContext(contextType, domain))
  {
    using (UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(pc, username))
    {
      if (userPrincipal != null)
      {
        try { userPrincipal.ChangePassword(oldPassword, newPassword); }
        catch (PasswordException pe) { return null; }
      }
    }
  }
}
Posted
Updated 17-Jun-16 3:29am
Comments
phil.o 17-Jun-16 8:42am    
If that's a local user, his account won't be in Active Directory.
Moreover, you are talking about a user, but you give the name of a computer. That's kind of confusing.

1 solution

Got it!

It was only a missing registry entry - RegisteredOrganization and RegisteredOwner!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900