【问题标题】:Change an Active Directory password [duplicate]更改 Active Directory 密码 [重复]
【发布时间】:2011-12-02 04:54:35
【问题描述】:

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

我正在开发一个管理 Active Directory 的网络平台。我可以创建、删除和编辑组、用户、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();

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

只有在用户尝试编辑自己的密码时才会失败。

任何帮助将不胜感激。

【问题讨论】:

    标签: c# active-directory passwords


    【解决方案1】:

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

    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;
        }
    }
    

    【讨论】:

    • 哇,我也第一次尝试!
    【解决方案2】:

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

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

    【讨论】:

    • 我在 Paolo 建议的时候尝试过这段代码,结果是一样的:.Invoke 问题。而且我使用 DirectoryServices.AccountManagement,错误说我必须检查密码策略(长度、复杂性等)。我已经禁用了它们。因此,问题可能在于 ACE 和通过编程更改自己的密码的权限。
    • 我尝试使用 SecurityDescriptor、AccessControlList 等编辑 ACE,但失败了。它经常发生:“安全ID结构无效”。我尝试使用 PrincipalContext 及其方法 UserCannotChangePassword = false,在此处 (stackoverflow.com/questions/1761312/active-directory-properties) 或此处 (msdn.microsoft.com/en-us/library/windows/desktop/…) 进行了解释,但它似乎没用。
    • 发生的错误与此处相同:stackoverflow.com/questions/5946150/… 但该解决方案对我不起作用。
    【解决方案3】:

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

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

    【讨论】:

    • 感谢您的回复。我尝试了您的解决方案,但它失败了。使用 DirectoryEntry 时,会导致相同的 .Invoke 问题。当我使用 PrincipalContext 方式时,错误提示我必须检查密码策略(长度、复杂性等)。我已经禁用了它们。 O.o
    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-10
      • 2015-09-11
      • 1970-01-01
      • 2015-03-17
      • 2013-09-11
      • 2012-06-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多