Change AD Password WebPart
using ActiveDs;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System;
using System.DirectoryServices;
using System.Web.UI.WebControls;
namespace ChangePassword
{
public class ChangePasswordWebpart : System.Web.UI.WebControls.WebParts.WebPart
{
private TextBox oldpassword;
private TextBox newpassword;
private TextBox checknewpassword;
private LinkButton btn;
private Label output;
protected override void CreateChildControls()
{
this.oldpassword = new TextBox();
this.oldpassword.TextMode = TextBoxMode.Password;
this.Controls.Add(oldpassword);
this.newpassword = new TextBox();
this.newpassword.TextMode = TextBoxMode.Password;
this.Controls.Add(newpassword);
this.checknewpassword = new TextBox();
this.checknewpassword.TextMode = TextBoxMode.Password;
this.Controls.Add(checknewpassword);
this.btn = new LinkButton();
this.btn.Click += new EventHandler(btn_Click);
this.btn.Text = "Change Password";
this.Controls.Add(btn);
this.output = new Label();
this.Controls.Add(output);
base.CreateChildControls();
}
void btn_Click(object sender, EventArgs e)
{
if (newpassword.Text.ToString() == checknewpassword.Text.ToString())
{
SPWeb webContext = SPControl.GetContextWeb(Context);
string strLoginName = webContext.CurrentUser.LoginName;
int iPosition = strLoginName.IndexOf("\\") + 1;
strLoginName = strLoginName.Substring(iPosition);
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();
if (results.Count > 0)
{
foreach (SearchResult result in results)
{
try
{
entry = result.GetDirectoryEntry();
}
catch (Exception error) { output.Text += "<BR>" + error.Message.ToString(); }
}
try
{
entry.Invoke("ChangePassword", new object[] { oldpassword.Text.ToString(), newpassword.Text.ToString() });
entry.CommitChanges();
output.Text += "<BR> Password is changed";
}
catch (Exception)
{
output.Text += "<b> Password couldn't be changed due to restrictions<b>";
}
}
else
{
output.Text += "<BR> User not found or bad password";
}
}
else
{
output.Text += "<BR>Passwords don't match";
}
}
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
string strLoginName = string.Empty;
try
{
SPWeb webContext = SPControl.GetContextWeb(Context);
strLoginName = webContext.CurrentUser.LoginName;
}
catch (Exception)
{
output.Text += "<BR> Please sign in first using the 'Sign In' button above";
}
if (strLoginName != string.Empty)
{
writer.Write("<table border=0>");
writer.Write("<tr>");
writer.Write("<td class='ms-vb'>");
writer.Write("Current password:");
writer.Write("</td>");
writer.Write("<td class='ms-vb'>");
oldpassword.RenderControl(writer);
writer.Write("</td>");
writer.Write("<td class='ms-vb'>");
writer.Write("</td>");
writer.Write("</tr>");
writer.Write("<tr valign='top'>");
writer.Write("<td class='ms-vb'>");
writer.Write("New password:");
writer.Write("</td>");
writer.Write("<td class='ms-vb'>");
newpassword.RenderControl(writer);
writer.Write("</td>");
writer.Write("<td class='ms-vb'>");
writer.Write("</td>");
writer.Write("</tr>");
writer.Write("<tr valign='top'>");
writer.Write("<td class='ms-vb'>");
writer.Write("Confirm new password:");
writer.Write("</td>");
writer.Write("<td class='ms-vb'>");
checknewpassword.RenderControl(writer);
writer.Write("</td>");
writer.Write("<td class='ms-vb'>");
writer.Write("</tr>");
writer.Write("<tr valign='top'>");
writer.Write("</td>");
writer.Write("<td class='ms-vb'>");
writer.Write("</td>");
writer.Write("<td class='ms-vb'>");
btn.RenderControl(writer);
writer.Write("</td>");
writer.Write("<td class='ms-vb'>");
writer.Write("</td>");
writer.Write("</tr>");
writer.Write("</table>");
output.RenderControl(writer);
}
else { output.RenderControl(writer); }
}
}
}You have to reference the "System.