1.通过vs 2013 新建一个web站点(不是空项目),这个会带一下模板,
2.然后新建一个页面UpdatePassWord.aspx
aspx页面内容:
<%@ Page Title="UpdatePassWord" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="UpdatePassWord.aspx.cs" Inherits="ADUpdatePwd.Account.UpdatePassWord" %> <asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent"> <hgroup class="title"> <h1><%: Title %>.</h1> <h2>更改AD域中用户密码。</h2> </hgroup> <div> <p class="message-info"> 密码必须至少包含 <%: Membership.MinRequiredPasswordLength %> 个字符。 </p> <p class="validation-summary-errors"> <asp:Literal runat="server" ID="ErrorMessage" /> </p> <fieldset> <legend>修改密码</legend> <ol> <li> <asp:Label runat="server" AssociatedControlID="txt_UserName">用户名</asp:Label> <asp:TextBox runat="server" ID="txt_UserName" />@sharepoint.com.cn <asp:RequiredFieldValidator runat="server" ControlToValidate="txt_UserName" CssClass="field-validation-error" ErrorMessage="用户名字段是必填字段。" /> </li> <li> <asp:Label runat="server" AssociatedControlID="txt_Old_PassWord">旧密码</asp:Label> <asp:TextBox runat="server" ID="txt_Old_PassWord" TextMode="Password" /> <asp:RequiredFieldValidator runat="server" ControlToValidate="txt_Old_PassWord" CssClass="field-validation-error" ErrorMessage="旧密码字段是必填字段。" /> </li> <li> <asp:Label runat="server" AssociatedControlID="txt_New_Password">新密码</asp:Label> <asp:TextBox runat="server" ID="txt_New_Password" TextMode="Password" /> <asp:RequiredFieldValidator runat="server" ControlToValidate="txt_New_Password" CssClass="field-validation-error" ErrorMessage="密码字段是必填字段。" /> </li> <li> <asp:Label runat="server" AssociatedControlID="txt_New_ConfirmPassword">确认新密码</asp:Label> <asp:TextBox runat="server" ID="txt_New_ConfirmPassword" TextMode="Password" /> <asp:RequiredFieldValidator runat="server" ControlToValidate="txt_New_ConfirmPassword" CssClass="field-validation-error" Display="Dynamic" ErrorMessage="确认新密码字段是必填字段。" /> <asp:CompareValidator runat="server" ControlToCompare="txt_New_Password" ControlToValidate="txt_New_ConfirmPassword" CssClass="field-validation-error" Display="Dynamic" ErrorMessage="新密码和确认新密码不匹配。" /> </li> </ol> <asp:Button runat="server" ID="btn_Action_Update" CommandName="MoveNext" Text="修改" OnClick="btn_Action_Update_Click" /> </fieldset> </div> </asp:Content>
cs:代码:
using System; using System.Collections.Generic; using System.Configuration; using System.DirectoryServices; using System.IO; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace ADUpdatePwd.Account { public partial class UpdatePassWord : System.Web.UI.Page { static string _exception = ""; protected void Page_Load(object sender, EventArgs e) { } /// <summary> /// 修改密码 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btn_Action_Update_Click(object sender, EventArgs e) { string _filePath = "log_" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";//输出日志 int _flog = 1;//记录标识,是否存在修改密码的账户 1:代表存在此用户 2:代表不存在此用户 3:代表程序出错 4:代表旧密码不正确 5:新密码和旧密码相同 _filePath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + _filePath; CheckIsHaveFile(_filePath); WriteLogToTxt(_filePath, "----执行开始----"); string _UserName = txt_UserName.Text.Trim(); string _Old_PassWord = txt_Old_PassWord.Text.Trim(); string _New_Password = txt_New_Password.Text.Trim(); string _New_ConfirmPassword = txt_New_ConfirmPassword.Text.Trim(); string DomainServer = ConfigurationManager.AppSettings["DomainServer_AD"].ToString(); string ManagerName = ConfigurationManager.AppSettings["ManagerName_AD"].ToString(); string ManagerPassword = ConfigurationManager.AppSettings["ManagerPassword_AD"].ToString(); WriteLogToTxt(_filePath, "----域控服务器:----" + DomainServer); WriteLogToTxt(_filePath, "----管理员账户:----" + ManagerName); WriteLogToTxt(_filePath, "----管理员密码:----" + ManagerPassword); WriteLogToTxt(_filePath, "----被修改账户:----" + _UserName); WriteLogToTxt(_filePath, "----旧密码:----" + _Old_PassWord); WriteLogToTxt(_filePath, "----新密码:----" + _New_Password); if (_Old_PassWord != _New_Password) { if (CheckIs_Old_PassWord_IsCorrect(_UserName, _Old_PassWord)) { #region 密码修改部分 DirectoryEntry entry = new DirectoryEntry("LDAP://" + DomainServer, ManagerName, ManagerPassword, AuthenticationTypes.Secure); List<string> directorys = new List<string>(); try { if (entry != null) { DirectorySearcher mySearcher = new DirectorySearcher(entry); mySearcher.Filter = ("(&(objectClass=user)(sAMAccountName=" + _UserName + "))"); SearchResult searchResult = mySearcher.FindOne(); if (searchResult != null) { _flog = 1; WriteLogToTxt(_filePath, "----是否存在:----域中存在此账户"); DirectoryEntry userEntry = searchResult.GetDirectoryEntry(); //userEntry.Invoke("ChangePassword", new object[] { _Old_PassWord, _New_ConfirmPassword });//调ChangePassword方法修改密码 userEntry.Invoke("SetPassword", new object[] { _New_ConfirmPassword }); entry.CommitChanges();//提交修改 WriteLogToTxt(_filePath, "----执行结果:----密码修改成功"); } else { _flog = 0; WriteLogToTxt(_filePath, "----是否存在:----域中不存在此账户"); } } } catch (Exception ex) { WriteLogToTxt(_filePath, "----程序报错:----" + ex.Message); ErrorMessage.Text = "程序报错:" + ex.Message; _exception = ex.Message; _flog = 3; } finally { if (entry != null) { //释放资源 entry.Close(); entry.Dispose(); } } #endregion } else _flog = 4; } else { _flog = 5; } WriteLogToTxt(_filePath, "----执行结束----"); if (_flog == 5) { ErrorMessage.Text = "温馨提示:旧密码和新密码不能相同!"; } else if (_flog == 4) { ErrorMessage.Text = "程序报错:" + _exception; } else if (_flog == 0) { ErrorMessage.Text = "温馨提示:域中不存在此账户!"; } else if (_flog == 1) { ErrorMessage.Text = "温馨提示:修改成功!"; } else if (_flog == 3) { ErrorMessage.Text = "程序报错:" + _exception; } } /// <summary> /// 检查输入的用户和老密码是否正确 /// </summary> /// <param name="userName"></param> /// <param name="passWord"></param> /// <returns></returns> public static bool CheckIs_Old_PassWord_IsCorrect(string userName, string passWord) { string _filePath = "log_" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";//输出日志 int _temp = 0; _filePath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + _filePath; string DomainServer = ConfigurationManager.AppSettings["DomainServer_AD"].ToString(); string path = "LDAP://" + DomainServer; DirectoryEntry entry = new DirectoryEntry(path, userName, passWord, AuthenticationTypes.Secure); DirectorySearcher mySearcher = new DirectorySearcher(entry); mySearcher.Filter = ("(SAMAccountName=" + userName + ")"); try { SearchResult searchResult = mySearcher.FindOne(); if (searchResult != null) _temp = 1; } catch (Exception ex) { WriteLogToTxt(_filePath, "----程序报错:----" + ex.Message); _exception = ex.Message; _temp = 0; } if (_temp == 1) return true; else return false; } /// <summary> /// 判断是否存在文件 /// </summary> /// <param name="_filePath"></param> public static void CheckIsHaveFile(string _filePath) { if (!System.IO.File.Exists(_filePath)) { System.IO.File.WriteAllText(_filePath, ""); } } /// <summary> /// 写入txt文件记录信息 /// </summary> /// <param name="_filePath"></param> /// <param name="strdata"></param> public static void WriteLogToTxt(string _filePath, string strdata) { StreamWriter sw = System.IO.File.AppendText(@_filePath); sw.WriteLine(DateTime.Now.ToLocalTime() + " " + strdata); sw.Flush(); sw.Close(); } } }
web.config
<!--人员 域控 服务器--> <add key="DomainServer_AD" value="192.168.1.1"/> <!--管理员账号--> <add key="ManagerName_AD" value="administrator"/> <!--管理员密码--> <add key="ManagerPassword_AD" value="1qaz2wsxR"/>