0

I have problem with c# script who change user AD password, when try change password, they throw exception

A constraint violation occurred. (Exception from HRESULT: 0x8007202F)

Code

DirectoryEntry entry = new DirectoryEntry("LDAP://domain.com", strLoginName, oldpassword.Text.ToString(), AuthenticationTypes.Secure);
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(SAMAccountName=" + strLoginName + ")";
search.SearchScope = SearchScope.Subtree;
search.CacheResults = false;

SearchResultCollection results = search.FindAll();
foreach (SearchResult result in results)
    entry = result.GetDirectoryEntry();

entry.Invoke("ChangePassword", new object[] { oldpassword.Text.ToString(), newpassword.Text.ToString() });
entry.CommitChanges();

Which could be a problem?

valch
  • 577
  • 2
  • 7
  • 17

1 Answers1

3

Look here: https://stackoverflow.com/a/1066177/1027551

For your example it would look like this:

using (var context = new PrincipalContext(ContextType.Domain,"domain.com"))
using (var user = UserPrincipal.FindByIdentity( context, IdentityType.SamAccountName, 
                                                                 strLoginName )) 
{ 
  user.ChangePassword( oldPassword, newpassword ); 
} 

I hope that helps.

Liam
  • 27,717
  • 28
  • 128
  • 190
Daro
  • 1,990
  • 2
  • 16
  • 22
  • By the way. I believe your problem is that your passing an array to a single value attribute. Also you are querying SamAccountName, but using FindAll instead of FindOne. If you have more than one user with the same SamAccountName, you have more trouble, than just the 0x8007202F exception... – Daro Mar 28 '12 at 22:51