I am trying to change the Active Directory password using C#. However, it seems to me it does not really works. I wonder what is wrong with my code. Hope somene can help me with this and thanks in advance. Below is my code :
try
{
string dcDNS = "mypsptestdev"; //use this if you want to supply a server name
DirectoryEntry userEntry = null;
string currentUserName = (((string)Context.User.Identity.Name).Split('\\')) [1];
DirectoryEntry rootDSE = new DirectoryEntry(String.Format("LDAP://{0}/rootDSE", dcDNS), "_aduser", "TESTsql@dm1n", AuthenticationTypes.Secure);
string rootDN = rootDSE.Properties["defaultNamingContext"].Value.ToString();
DirectoryEntry searchRoot = new DirectoryEntry(String.Format("LDAP://{0}/{1}", dcDNS, rootDN), "_aduser", "TESTsql@dm1n", AuthenticationTypes.Secure);
DirectorySearcher searcher = new DirectorySearcher(searchRoot);
searcher.Filter = String.Format("samAccountName={0}", "_aduser");
searcher.SearchScope = SearchScope.Subtree;
SearchResultCollection results = searcher.FindAll();
foreach (SearchResult result in results)
{
userEntry = result.GetDirectoryEntry();
break;
}
if (userEntry == null)
{
throw new InvalidOperationException("User not found in this domain.");
}
userEntry.Invoke("ChangePassword", new object[]{"TESTsql@dm1n", "sql@dm1n"});
userEntry.CommitChanges();
}
catch (System.Reflection.TargetInvocationException ex)
{
Response.Write("msg1 = " + ex.ToString() + "<br>");
Response.Write("msg2 = " + ex.InnerException.ToString());
}
And this is the error i got :
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Runtime.InteropServices.COMException (0x8007202F): A constraint violation occurred. --- End of inner exception stack trace --- at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters) at System.RuntimeType.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParameters) at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args) at System.DirectoryServices.DirectoryEntry.Invoke(Str ing methodName, Object[] args) at AdminChangePasswd.test.ChangePassword1() in c:\portal administration\adminchangepasswd\test.aspx.cs:line 246
System.Runtime.InteropServices.COMException (0x8007202F): A constraint violation occurred.