Changing Passwords With Active Directory Can Sometimes be very
(painful).
I have used this code many times in the past, note the ldap path..
1
2 if (newpassword.Text.ToString() == checknewpassword.Text.ToString())
3 {
4 // Just fetch the user's XXXX\xxx login name
5 SPWeb webContext = SPControl.GetContextWeb(Context);
6 string strLoginName = webContext.CurrentUser.LoginName;
7
8 int iPosition = strLoginName.IndexOf("\\") + 1;
9 strLoginName = strLoginName.Substring(iPosition);
10
11 DirectoryEntry entry = new DirectoryEntry("LDAP://blfoley.local", strLoginName, oldpassword.Text.ToString(),AuthenticationTypes.Secure);
12 DirectorySearcher search = new DirectorySearcher(entry);
13
14 search.Filter = "(SAMAccountName=" + strLoginName + ")";
15 search.SearchScope = SearchScope.Subtree;
16
17 search.CacheResults = false;
18
19 SearchResultCollection results = search.FindAll();
20
21 if (results.Count > 0)
22 {
23 foreach (SearchResult result in results)
24 {
25 try
26 {
27 entry = result.GetDirectoryEntry();
28 }
29 catch (Exception error)
30 {
31 output.Text += "" + error.Message.ToString();
32 }
33 }
34 try
35 {
36 entry.Invoke("ChangePassword", new object[] { oldpassword.Text.ToString(), newpassword.Text.ToString() });
37 entry.CommitChanges();
38 output.Text += "Password is changed";
39 }
40 catch (Exception)
41 {
42 output.Text += " Password couldn't be changed due to restrictions<b>";
43 }
44 }
45 else
46 {
47 output.Text += "<BR> User not found or bad password";
48 }
49 }
50 else
51 {
52 output.Text += "<BR>Passwords don't match";
53 }
Brad Foley
Web Developer
http://www.blfoley.com--------------------------
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.