在使用AD域环境搭建的账号系统修改密码的时候比较麻烦一般需要管理员在域环境去进行对用户的密码进行修改。
以下就是用来查询和修改AD域密码的方法。
1 /// <summary> 2 /// AD账户查询 3 /// </summary> 4 /// <param name="Domain"></param> 5 /// <param name="UserAccount"></param> 6 /// <param name="UserPassWord"></param> 7 /// <returns></returns> 8 private static string CheckAccount(string Domain, string UserAccount, string UserPassWord) 9 { 10 string ReturnValue = string.Empty; 11 try 12 { 13 //throw new InvalidPluginExecutionException(Domain + "|==|" + UserAccount + "|==|" + UserPassWord); 14 DirectoryEntry entry = new DirectoryEntry("LDAP://" + Domain, UserAccount, UserPassWord, AuthenticationTypes.Secure); 15 DirectorySearcher mySearcher = new DirectorySearcher(entry); 16 mySearcher.Filter = ("(&(objectClass=user)(sAMAccountName=" + UserAccount + "))"); 17 SearchResult searchResult = mySearcher.FindOne(); 18 if (searchResult != null) 19 { 20 ReturnValue = "1"; 21 } 22 } 23 catch (Exception ex) 24 { 25 ReturnValue = "账号有误:" + ex.Message; 26 } 27 return ReturnValue; 28 }
/// <summary> /// 修改AD域密码 /// </summary> /// <param name="Domain">域</param> /// <param name="AdministratorAccount">管理员账号</param> /// <param name="UserAccount">待修改密码账户</param> /// <param name="AdministratorPassWord">管理员账户密码</param> /// <param name="UserNewPassWord">新用户新密码</param> /// <param name="User_ConfirmPassword">新用户新密码确认</param> /// <returns></returns> private static string UpdateADAccount(string Domain, string AdministratorAccount, string UserAccount, string AdministratorPassWord, string UserNewPassWord, string User_ConfirmPassword) { //反馈内容、 string ReturnString = string.Empty; if (UserNewPassWord == User_ConfirmPassword) { try { DirectoryEntry entry = new DirectoryEntry("LDAP://" + Domain, AdministratorAccount, AdministratorPassWord, AuthenticationTypes.Secure); DirectorySearcher mySearcher = new DirectorySearcher(entry); mySearcher.Filter = ("(&(objectClass=user)(sAMAccountName=" + UserAccount + "))"); SearchResult searchResult = mySearcher.FindOne(); DirectoryEntry userEntry = searchResult.GetDirectoryEntry(); userEntry.Invoke("SetPassword", new object[] { UserNewPassWord }); ReturnString = "修改密码成功!!"; } catch (Exception ex) { ReturnString = "修改密码出错!!问题可能是:" + ex.Message; } } else { ReturnString = "两次密码输入不一致"; } return ReturnString; }