470,821 Members | 1,997 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,821 developers. It's quick & easy.

Change password in AD

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 :

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();

if (userEntry == null)
throw new InvalidOperationException("User not found in this domain.");

userEntry.Invoke("ChangePassword", new object[]{"TESTsql@dm1n", "sql@dm1n"});

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.
Apr 10 '06 #1
0 5046

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by GregS | last post: by
1 post views Thread by eighthman11 | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.