首页 > 解决方案 > 如何使用 C# 从 ASP.Net 重置 Active Directory 用户的密码?

问题描述

我要做的是通过填写表格来重置用户的密码,然后单击“更改密码”按钮。

因此,在我的 aspx 页面中,我有 3TextBoxes个用户名,一个用于密码,一个用于确认密码。

所以基本上它需要找到在文本框中输入的用户名,然后根据在其他两个文本框中输入的内容更改密码。

因此,如果我填写表格并选择“更改密码”,我会收到以下错误:

 Exception has been thrown by the target of an invocation.

这是我目前正在使用的代码:

 try
        {
            string strADPath = "LDAP";
            string strUsername = "user";
            string strPassword = "LDAPPassword";
            string userName = txtUserName.Text;
            string newPassword = txtNewPassword.Text;
            PrincipalContext pc = new PrincipalContext(ContextType.Domain, strADPath, strUsername, strPassword);
            UserPrincipal user = UserPrincipal.FindByIdentity(pc, userName);
            if (user == null)
            {
                error.Text = "User name not found in this domain.";
            }

            user.SetPassword(newPassword);
            error.Text = "Password for " + userName + "changed successfully.";
        }
        catch (Exception ex)
        {
            // throw exception here
            error.Text = ex.Message;
        }

不知道为什么我会收到这个错误?

谢谢

标签: c#asp.net

解决方案


推荐阅读