2017-05-08 73 views
0

我有一个按钮,它通过各种验证代码单击事件运行,然后调用方法`ChangePassword',如果您通过验证。处理与Active Directory更改密码相关的错误问题

public void ChangePassword(string userName, string oldPassword, string newPassword) 
    { 
     try 
     { 
      new ApplusActiveDirectoryUtil().CheckParameter(ref userName, true, true, false, 21, "User Name"); 

      DirectoryEntry userEntry = _directoryInfo.GetUserEntry(userName); 
      userEntry.Invoke("ChangePassword", new Object[] { oldPassword, newPassword }); 
      //unlock account 
      userEntry.Properties["LockOutTime"].Value = 0x0000; 
      userEntry.CommitChanges(); 
      userEntry.Dispose(); 
      userEntry.Close(); 
     } 
     catch (Exception ex) 
     { 
      _directoryInfo.Initialize(); 
      DirectoryEntry domainEntry = _directoryInfo.DomainDirectoryEntry; 
      ApplusActiveDirectoryDomainPolicy domainPolicy = new ApplusActiveDirectoryDomainPolicy(_directoryInfo.DomainDirectoryEntry); 
      string message = "Password entered was wrong or password entered was the same as the previous " + domainPolicy.PasswordHistoryLength + " passwords set."; 
      throw new Exception(message, ex); 
     } 
    } 

我遇到的问题是,这条线......

userEntry.Invoke("ChangePassword", new Object[] { oldPassword, newPassword }); 

会给出错误...

System.Runtime.InteropServices.COMException(0x80070056) : 指定的网络密码不正确。

当用户在“当前密码”文本框中输入密码但与当前密码不匹配时,会发生此错误。

我试图做处理这个..

if (txtConfirmNewPassword.Text != user.Password) 
    { 
     SetChangePasswordMessage("Password entered was wrong"); 
    } 

但是从我读过,这是不可能检索用户的AD密码。

可以优雅地处理这个错误而不必捕捉它?

+1

如何验证凭证第一,如果通过然后继续并调用密码更改。 – Equalsk

+0

就是这样!我已经掌握了你的建议。谢谢 – MadDev

回答

0

感谢Equalsk评论我找到了解决方案。

我需要先验证凭据。如果凭证有效,请继续并调用ChangePassword方法。

  bool IsValidate = Membership.ValidateUser(user.UserName, txtOldPassword.Text); 
      if (!IsValidate) 
      { 
       SetChangePasswordMessage("Password entered was wrong or password entered was the same as the previous " + domain.PasswordHistoryLength + " passwords set."); 
      } 
      else 
      { 
       new ApplusActiveDirectoryMembership(admin.AdminADUserName, admin.AdminADPassword).ChangePassword(user.UserName, txtOldPassword.Text, txtConfirmNewPassword.Text); 
       SetChangePasswordMessage("The password has been successfully changed."); 
      } 
相关问题