【问题标题】:C# Active Directory Invoke "ChangePassword" cannot contact domainC# Active Directory 调用“ChangePassword”无法联系域
【发布时间】:2016-12-31 16:00:00
【问题描述】:

我有以下代码作为我的 Active Directory 用户的 Web 应用程序的一部分,以便能够更新他们的密码(同时用于 Active Directory 和 gmail)。我将 C# 与 System.DirectoryServices.AccountManagement 一起使用。

这段代码一直有效到昨天

try
{
    State.log.WriteLine("Connecting LDAP.");
    string ldapPath = "LDAP://192.168.76.3";
    DirectoryEntry directionEntry = new DirectoryEntry(ldapPath, domainName + "\\" + userName, currentPassword);
    if (directionEntry != null)
    {
        DirectorySearcher search = new DirectorySearcher(directionEntry);
        State.log.WriteLine("LDAP Connected, searching directory for SAMAccountName");
        search.Filter = "(SAMAccountName=" + userName + ")";
        SearchResult result = search.FindOne();
        if (result != null)
        {
            State.log.WriteLine("Getting User Entry.");
            DirectoryEntry userEntry = result.GetDirectoryEntry();
            if (userEntry != null)
            {
                State.log.WriteLine("Setting Password");
                if (force)
                {
                    userEntry.Invoke("SetPassword", new[] { newPassword });
                }
                else
                {
                    userEntry.Invoke("ChangePassword", new object[] { currentPassword, newPassword });
                }
                userEntry.CommitChanges();
                State.log.WriteLine("Changes Committed to ActiveDirectory.");
            }
            else
            {
                State.log.WriteLine("Could not get user Entry...");
            }
        }
        else
        {
            State.log.WriteLine("Search returned no results.");
        }
    }
    else
    {
        State.log.WriteLine("Could not connect to LDAP with given username and passwd");
    }
}

从昨天开始,这段代码就到了:

userEntry.Invoke("ChangePassword", new object[] { currentPassword, newPassword });

然后抛出如下异常:

[8:37:00 AM]:满足密码要求。

[8:37:00 AM]:正在连接 LDAP。

[8:37:00 AM]:LDAP 已连接,正在搜索 SAMAccountName 的目录

[8:37:01 AM]:获取用户条目。

[8:37:01 AM]:设置密码

[8:37:01 AM] : 无法为 jason 重置 Windows 密码。


调用的目标已抛出异常。


系统无法联系域控制器来处理身份验证请求。请稍后再试。 (HRESULT 异常:0x800704F1)

使用“SetPassword”的“force”选项仍然可以正常工作,但非管理员用户可以调用的“ChangePassword”方法则不行。

【问题讨论】:

    标签: c# active-directory directoryservices change-password account-management


    【解决方案1】:

    更改 userPrincipal.ChangePassword("旧通行证", "新通行证");给userPrincipal.SetPassword(model.NewPassword);

    【讨论】:

    • 没有管理权限的用户conetext不能调用SetPassword。
    【解决方案2】:

    我找到了解决方法,但忘记发布了。我所做的是使用上面的代码对用户进行身份验证,然后调用我的“ForceChangePassword”方法:

    public static void ForceChangeADPassword(String username, String newPassword)
    {
        String DN = "";
        try
        {
            DN = GetObjectDistinguishedName(objectClass.user, returnType.distinguishedName, username, DOMAIN_CONTROLLER_IP);
        }
        catch(Exception e)
        {
            throw new PasswordException(String.Format("Could not find AD User {0}", username), e);
        }
    
        if(DN.Equals(""))
            throw new PasswordException(String.Format("Could not find AD User {0}", username));
    
        DirectoryEntry userEntry = new DirectoryEntry(DN.Replace("LDAP://", LdapRootPath), "accounts", AcctPwd);
        userEntry.Invoke("SetPassword", new object[] { newPassword });
        userEntry.Properties["LockOutTime"].Value = 0;
    
        userEntry.CommitChanges();
        userEntry.Close();
    }
    

    【讨论】:

      【解决方案3】:

      本月初微软released a security patch,解决了密码更改领域的一些漏洞。具体来说,更新阻止了在更改密码时 Kerberos 身份验证失败后回退到 NTLM 身份验证。

      您可能想了解更多关于更新here

      【讨论】:

        【解决方案4】:

        微软更新了这篇文章:https://support.microsoft.com/en-us/kb/3177108。在这里,他们为我们提供了由原始“修复”造成的问题,以及使用 Kerberos 和自助密码重置的一些技巧。

        自 2016 年 10 月 11 日起,Microsoft 重新发布了与 https://technet.microsoft.com/en-us/library/security/ms16-101.aspx 相关的补丁,以解决由原始更新引起的问题(您可以在 https://support.microsoft.com/en-us/kb/3177108 中阅读,包括您无法再更改本地帐户密码的事实)。

        【讨论】:

          猜你喜欢
          • 2021-11-21
          • 1970-01-01
          • 1970-01-01
          • 2016-06-18
          • 1970-01-01
          • 2017-01-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多