//public static PasswordChangeResult ChangePassword(string userName, string oldPassword, string newPassword) //{ // return ChangePassword(string.Empty, userName, oldPassword, newPassword); //} public static PasswordChangeResult ChangePassword(string fqdn, string userName, string oldPassword, string newPassword) { PasswordChangeResult returnValue = new PasswordChangeResult(); //if (string.IsNullOrEmpty(fqdn)) // throw new ArgumentNullException("fqdn (fully qualified domain name)"); if (string.IsNullOrEmpty(userName)) { throw new ArgumentNullException("userName"); } if (string.IsNullOrEmpty(oldPassword)) { throw new ArgumentNullException("oldPassword"); } if (string.IsNullOrEmpty(newPassword)) { throw new ArgumentNullException("newPassword"); } returnValue.UserName = userName; //domainSlashUserName.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries)[1]; returnValue.Domain = fqdn; //domainSlashUserName.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries)[0]; try { DirectoryEntry searchRoot = new DirectoryEntry("LDAP://" + fqdn, userName, oldPassword, AuthenticationTypes.Secure); ADUser u = new ADUser(userName, searchRoot); //DirectoryEntry entry = new DirectoryEntry(u.DirectoryEntry.Path, userName, oldPassword, AuthenticationTypes.Secure); u.DirectoryEntry.Invoke("ChangePassword", oldPassword, newPassword); u.DirectoryEntry.CommitChanges(); returnValue.Status = PasswordChangeResultStatus.Success; } catch (COMException cex) { returnValue.ErrorMessage = cex.Message; returnValue.StackTrace = cex.StackTrace; returnValue.Status = PasswordChangeResultStatus.Error; } catch (Exception ex) { returnValue.ErrorMessage = ex.Message; returnValue.StackTrace = ex.StackTrace; returnValue.Status = PasswordChangeResultStatus.Error; } return(returnValue); }
//public static PasswordChangeResult ChangePassword(string userName, string oldPassword, string newPassword) //{ // return ChangePassword(string.Empty, userName, oldPassword, newPassword); //} public static PasswordChangeResult ChangePassword(string fqdn, string userName, string oldPassword, string newPassword) { PasswordChangeResult returnValue = new PasswordChangeResult(); //if (string.IsNullOrEmpty(fqdn)) // throw new ArgumentNullException("fqdn (fully qualified domain name)"); if (string.IsNullOrEmpty(userName)) throw new ArgumentNullException("userName"); if (string.IsNullOrEmpty(oldPassword)) throw new ArgumentNullException("oldPassword"); if (string.IsNullOrEmpty(newPassword)) throw new ArgumentNullException("newPassword"); returnValue.UserName = userName;//domainSlashUserName.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries)[1]; returnValue.Domain = fqdn;//domainSlashUserName.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries)[0]; try { DirectoryEntry searchRoot = new DirectoryEntry("LDAP://" + fqdn, userName, oldPassword, AuthenticationTypes.Secure); ADUser u = new ADUser(userName, searchRoot); //DirectoryEntry entry = new DirectoryEntry(u.DirectoryEntry.Path, userName, oldPassword, AuthenticationTypes.Secure); u.DirectoryEntry.Invoke("ChangePassword", oldPassword, newPassword); u.DirectoryEntry.CommitChanges(); returnValue.Status = PasswordChangeResultStatus.Success; } catch (COMException cex) { returnValue.ErrorMessage = cex.Message; returnValue.StackTrace = cex.StackTrace; returnValue.Status = PasswordChangeResultStatus.Error; } catch (Exception ex) { returnValue.ErrorMessage= ex.Message; returnValue.StackTrace = ex.StackTrace; returnValue.Status = PasswordChangeResultStatus.Error; } return returnValue; }