Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I want to write a webpart to change/reset a pasword to Active Directory. How Could I do it ?

Where are the user password are situated ?

share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted
DirectoryEntry myADUser = new
DirectoryEntry("LDAP://CN=User,CN=Users,DC=Domain,DC=com");
myADUser.Invoke("<opass>", "<npass>");
myADUser.CommitChanges();

How to change active directory password from webpart?

or

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.DirectoryServices" and the COM "Active DS Type Library", also when you package everything together, make sure that the Interop.ActiveDs.dll is included otherwise it doesn't work.

And at runtime it looks like this:

change ad password

http://glorix.blogspot.co.uk/2007/10/ad-change-password-webpart.html

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.