更改 Active Directory 密码

发布于 2024-12-08 04:53:04 字数 659 浏览 0 评论 0原文

首先,请原谅我的英语,它不是我的母语。

我正在开发一个管理 Active Directory 的 Web 平台。我可以创建、删除和编辑组、用户、OU 等。

当连接的用户想要通过平台更改自己的密码时,会失败。

它来自DirectoryEntry.Invoke

我使用了DirectoryServices.DirectoryEntry

directoryEntry.Invoke("SetPassword", password);
directoryEntry.Commit();

所以我尝试了System.DirectoryServices.AccountManagement:

PrincipalContext ctx = new PrincipalContext(ContextType.Machine);
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, Username);
user.SetPassword(password_);
user.Save();

不同的方式,同样的问题。

仅当用户尝试编辑自己的密码时,它才会失败。

任何帮助将不胜感激。

At first, please forgive my English, it is not my mother tongue.

I'm working on a web platform that manages Active Directory. I can create, delete and edit a group, user, OU, and so on.

When a connected user wants to change his own password with the platform, it fails.

It comes from DirectoryEntry.Invoke.

I used the DirectoryServices.DirectoryEntry:

directoryEntry.Invoke("SetPassword", password);
directoryEntry.Commit();

So I tried System.DirectoryServices.AccountManagement:

PrincipalContext ctx = new PrincipalContext(ContextType.Machine);
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, Username);
user.SetPassword(password_);
user.Save();

Different way, same problem.

It only fails when a user tries to edit his own password.

Any help would be grateful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

苦笑流年记忆 2024-12-15 04:53:05

更改密码需要用户的旧密码才能设置新密码,重置密码需要重置密码的人员权限。在 AD 的默认权限下,只有管理员和帐户操作员可以重置密码。

Change Password requires user's old password to set new password and Reset password permission requires to the person who resets the password.With AD's default permissions, only Administrators and Account Operators can reset passwords.

念﹏祤嫣 2024-12-15 04:53:04

试试这个代码。它对我有用,

public void ChangeMyPassword(string domainName, string userName, string currentPassword, string newPassword)
{
    try
    {
        string ldapPath = "LDAP://192.168.1.xx";
        DirectoryEntry directionEntry = new DirectoryEntry(ldapPath, domainName + "\\" + userName, currentPassword);
        if (directionEntry != null)

        {
            DirectorySearcher search = new DirectorySearcher(directionEntry);
            search.Filter = "(SAMAccountName=" + userName + ")";
            SearchResult result = search.FindOne();
            if (result != null)
            {
                DirectoryEntry userEntry = result.GetDirectoryEntry();
                if (userEntry != null)
                {
                    userEntry.Invoke("ChangePassword", new object[] { currentPassword, newPassword });
                    userEntry.CommitChanges();
                }
            }
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

Try this code. It works for me,

public void ChangeMyPassword(string domainName, string userName, string currentPassword, string newPassword)
{
    try
    {
        string ldapPath = "LDAP://192.168.1.xx";
        DirectoryEntry directionEntry = new DirectoryEntry(ldapPath, domainName + "\\" + userName, currentPassword);
        if (directionEntry != null)

        {
            DirectorySearcher search = new DirectorySearcher(directionEntry);
            search.Filter = "(SAMAccountName=" + userName + ")";
            SearchResult result = search.FindOne();
            if (result != null)
            {
                DirectoryEntry userEntry = result.GetDirectoryEntry();
                if (userEntry != null)
                {
                    userEntry.Invoke("ChangePassword", new object[] { currentPassword, newPassword });
                    userEntry.CommitChanges();
                }
            }
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}
ˇ宁静的妩媚 2024-12-15 04:53:04

正如 Paolo 所指出的,如果没有额外的权限,您就无法调用“重置密码”。要调用 ChangePassword,您需要提供以前的密码,如下所示:

directoryEntry.Invoke("ChangePassword", oldPassword, newPassword); 
directoryEntry.Commit(); 

As Paolo notes, you can't call Reset Password without extra privileges. To call ChangePassword, you need to supply the previous password like this:

directoryEntry.Invoke("ChangePassword", oldPassword, newPassword); 
directoryEntry.Commit(); 
寂寞笑我太脆弱 2024-12-15 04:53:04

这是 Windows 的一项限制:用户无法重置自己的密码,即在不提供旧密码的情况下更改密码。

您只能更改您自己的密码,即提供旧密码和新密码。
尝试改用 ChangePassword 方法。

This is a Windows restriction: a user cannot reset his own password, i.e. change the password without providing the old one.

You can only change your own password, i.e. provide old password and new password.
Try using the ChangePassword method instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文