gpt4 book ai didi

c# - 与Active Directory更改密码相关的处理错误问题

转载 作者:行者123 更新时间:2023-12-03 08:49:45 25 4
gpt4 key购买 nike

我有一个按钮,它的click事件通过各种验证代码运行,如果您通过验证,则调用方法“ChangePassword”。

    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): The specified network password is not correct.



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

我试图通过做来解决这个问题。
   if (txtConfirmNewPassword.Text != user.Password)
{
SetChangePasswordMessage("Password entered was wrong");
}

但根据我的阅读,无法检索用户的AD密码。

是否可以优雅地处理此错误而不必捕获它?

最佳答案

感谢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.");
}

关于c# - 与Active Directory更改密码相关的处理错误问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43849654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com