c# 更改 AD 密码 Directoryservices

标签 c# active-directory

我试图让下面的代码工作,问题是,有时它会,有时它不会。 当它失败时,它会给出错误 0x800704F1“系统无法联系域 Controller 来为身份验证请求提供服务” 我会说大约 90% 的时间它会失败。 我已经尝试通过将它添加到上下文类型后面来给它一个静态 DC,遗憾的是这没有帮助。 对于管理员用户,它始终有效。但是我相信用户应该能够更改自己的密码。 错误在 user.changepassword 行触发

我希望其他人有一个好主意。

        using (var context = new PrincipalContext(ContextType.Domain))
        {
            using (var user = UserPrincipal.Current)
            {
                try
                {
                    user.ChangePassword(txt_old.Text, txt_new.Text);
                    user.Save();

                }
                catch(Exception p)
                {
                    if (p.HResult.Equals("0x800708C5"))//Not secure enough according to password policy
                    {
                        MessageBox.Show("Volgens het systeem is uw nieuwe wachtwoord niet veilig genoeg, voldoet het aan alle eisen?", "Niet gelukt", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    else if (p.HResult.Equals("0x80070056")) //Wrong current password
                    {
                        MessageBox.Show("U heeft een verkeerd huidig wachtwoord ingevult, probeer het nogmaals", "Verkeerd wachtwoord", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    else if (p.InnerException.ToString().Contains("0x80070775")) //Temporarly locked out.
                    {
                        MessageBox.Show("Uw account is tijdelijk vergrendeld door te veel pogingen tot in te loggen met een foutief wachtwoord. Probeer het over 15minuten nogmaals of neem contact op met de helpdesk.", "vergrendeld.", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    else
                    {
                        MessageBox.Show(System.Security.Principal.WindowsIdentity.GetCurrent().Name + Environment.NewLine + p.HResult + Environment.NewLine + p.Message);
                        return;
                    }
                }
            }
        }

最佳答案

两个 Windows 更新 3177108 和 3167679 更改了 ChangePassword 的行为。

这里有一个关于这个问题的线程:https://social.msdn.microsoft.com/Forums/vstudio/en-US/77dc733e-a13d-4349-9088-8065b85d5c3f/userprincipalchangepassword-stops-working-after-windows-updates-3177108-and-3167679?forum=netfxbcl

看来,您现在必须在创建 PrincipalContext 时指定有效的 UPN。

之前在创建上下文时可以使用 IP 作为端点,现在看来它也必须是正确的域名。

此外,您现在总是在发生错误时收到相同的异常 - 我们过去常常收到用户选择密码不足的密码策略异常,现在我们得到:

System.DirectoryServices.AccountManagement.PrincipalOperationException: The system cannot contact a domain controller to service the authentication request. Please try again later. (Exception from HRESULT: 0x800704F1)

2016 年 4 月 10 日更新: 上面显示的异常实际上是在更新后调用 ChangePassword 时收到的几乎所有内容的一般/一般错误。 例如,如果协议(protocol)中涉及的某些端口被防火墙阻止,您也会得到这个(如果您从未加入域的服务器/机器调用则适用)。

所需端口的良好资源:https://technet.microsoft.com/en-us/library/dd772723(v=ws.10).aspx 请注意,动态范围也是必需的。

如果不允许用户更改密码(域策略,通过设置 MUST CHANGE AT NEXT LOGON FLAG 来规避),您也会收到此异常。

关于c# 更改 AD 密码 Directoryservices,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38969255/

相关文章:

active-directory - IIS Windows 身份验证拒绝某些用户

c# - 为什么 .NET 中没有 Tree<T> 类?

c# - C# 和 SQL Server 中 int 到 guid 的不同转换

c# - .NET - 使用多个 SqlParameters 将异常记录到数据库并指定每个数据库列中的内容

c# - 查询 Active Directory 的任务并行库(使用 DirectoryServices)

active-directory - 如何使用 Okta 的 API 获取用户所属的所有组?

powershell - 以不同用户身份从 PowerShell 打开远程位置

SHA256 的 C# SignatureDescription

c# - 哪个是异常处理的更好做法?

linux - Active Directory 上的 LDAP 搜索