【问题标题】:LDAP reset password from outside the domain network C# Error: RPC server is unavailable. (exception from hresult: 0x800706ba)LDAP 从域网络外部重置密码 C# 错误:RPC 服务器不可用。 (来自 hresult 的异常:0x800706ba)
【发布时间】:2020-05-29 13:43:46
【问题描述】:

我们正在尝试重置 LDAP 密码,它在开发环境中有效,但在生产环境中无效。

我们的开发环境在域内,生产环境在域外。

在连接LDAP的开发中,我们使用了abc.com这样的域名,我们使用IPaddress:389这样的生产环境,这已经在两个环境中都适用于LDAP用户身份验证。但不适用于 LDAP 重置密码。

Error: RPC server is unavailable. (exception from hresult: 0x800706ba)

发展:(工作)

PrincipalContext principalContext =
    new PrincipalContext(ContextType.Domain, "<domain.com>", container: "<DC=domain,DC=com>",
    "<username>", "<password>");
UserPrincipal user = UserPrincipal.FindByIdentity(principalContext, "<LdapUserName>");
// "<username>", "<password>" are Administrative credential.
bool isValid = user.ValidateCredentials("<username>", "<password>");
_logger.Log($"Is Connection: {isValid}");
**// Output: Is Connection: True**
user.UserCannotChangePassword = false;
user.SetPassword("<NewPassword>");
// Password has been successfully reset.

生产:(工作) 此外,我们使用以下方法对 LDAP 用户进行身份验证,它在生产中工作:

检查用户是否有 LDAP 帐户:

// "<username>", "<password>" are Administrative credential.
var entry = new DirectoryEntry($"LDAP://{"<IP:389>"}", "<username>", "<password>",
    AuthenticationTypes.Secure | AuthenticationTypes.Sealing | AuthenticationTypes.ServerBind);
var search = new DirectorySearcher(entry);
var strFilter = $"(mail={"<UserEmailId>"})";
search.Filter = strFilter;
var result = await Task.Run(() => search.FindOne());
if (result != null)
{
    //IsLdapUser = true;
    //result.Properties["samaccountname"][0]);
}
else
{
    //IsLdapUser = false;
}
// Successfully


// Authenticate LDAP user:
var ldapConnection = new LdapConnection(new LdapDirectoryIdentifier("<IP:389>", false, false));                    
var nc = new NetworkCredential("<LdapUserName>", "<LdapUserPassword>", "<IP:389>");
ldapConnection.Credential = nc;
ldapConnection.AuthType = AuthType.Negotiate;
ldapConnection.Bind(nc);
// Successfully

生产:(不工作)

// "<username>", "<password>" are Administrative credential.
PrincipalContext principalContext =
    new PrincipalContext(ContextType.Domain, "<IP:389>", container: "<DC=domain,DC=com>",
    "<username>", "<password>");
UserPrincipal user = UserPrincipal.FindByIdentity(principalContext, "<LdapUserName>");
bool isValid = user.ValidateCredentials("<username>", "<password>");
_logger.Log($"Is Connection: {isValid}");
**// Output: Is Connection: True**
user.UserCannotChangePassword = false;
user.SetPassword("<NewPassword>");
// Error: RPC server is unavailable. (exception from hresult: 0x800706ba)

也尝试使用以下代码(不工作)

// "<username>", "<password>" are Administrative credential.
DirectoryEntry de = new DirectoryEntry("<IP:389>","<username>", "<password>", 
AuthenticationTypes.Secure | AuthenticationTypes.Sealing | AuthenticationTypes.ServerBind);
// LDAP Search Filter
DirectorySearcher ds = new DirectorySearcher(de);
ds.Filter = "(&(objectClass=user)(|(sAMAccountName=" + "<LdapUserName>"+ ")))";

// LDAP Properties to Load
ds.PropertiesToLoad.Add("displayName");
ds.PropertiesToLoad.Add("sAMAccountName");
ds.PropertiesToLoad.Add("DistinguishedName");
ds.PropertiesToLoad.Add("CN");

// Execute Search
SearchResult result = await Task.Run(() => ds.FindOne());

string dn = result.Properties["DistinguishedName"][0].ToString();

DirectoryEntry uEntry = result.GetDirectoryEntry();

uEntry.Invoke("SetPassword", new object[] { "<NewPassword>"});  //Set New Password                        
uEntry.CommitChanges();
uEntry.Close();
// Error: RPC server is unavailable. (exception from hresult: 0x800706ba)

【问题讨论】:

    标签: c# asp.net asp.net-core authentication ldap


    【解决方案1】:

    用于修改密码的属性是unicodePwd。该文档揭示了更改密码必须满足的一些条件。首先,必须对连接进行加密。

    调用.Invoke("SetPassword", ...)实际上调用了本机Windows IADsUser::SetPassword方法。该文档显示它会自动尝试几种不同的加密方式。发生异常是因为这些方法都不起作用。

    您实际上可以直接修改unicodePwd 属性,而无需调用SetPassword,我会讲到,但无论如何,您必须先解决加密问题。

    当您从网络内的计算机运行此程序时,AuthenticationTypes.Sealing 就足够了。如the documentation says,效果是它使用Kerberos加密连接。

    但是,当您从域外部进行连接时,Kerberos 将无法工作(也许需要努力 - 我不是 Kerberos 专家)。所以唯一可用的加密方法是 SSL。 SetPassword 方法确实尝试使用 SSL,但显然它不起作用。

    我马上看到的一个问题是您使用 IP 地址连接到 DC,而 SSL 无法使用 IP 地址工作,因为 SSL 证书上的域名必须与您的名称匹配用于访问服务器,并且 SSL 证书上不会有 IP 地址。所以你必须改变它才能使用域名。如果 DNS 无法解析该名称,您可以将其添加到您的 hosts 文件中。

    更改可能可以解决所有问题。如果没有,可能还有另外两个问题:

    1. 端口 636 无法访问(防火墙挡住了)。
    2. SSL 证书在您运行它的计算机上不受信任

    LDAP over SSL (LDAPS) 在端口 636 上工作。您可以在 PowerShell 中测试此连接:

    Test-NetConnection example.com -Port 636
    

    如果失败,请先修复它。

    接下来,检查证书。您可以使用此 PowerShell 脚本下载证书:

    $webRequest = [Net.WebRequest]::Create("https://example.com:636")
    try { $webRequest.GetResponse() } catch {}
    $cert = $webRequest.ServicePoint.Certificate
    $bytes = $cert.Export([Security.Cryptography.X509Certificates.X509ContentType]::Cert)
    set-content -value $bytes -encoding byte -path "certificate.cer"
    

    将第一行中的example.com 更改为您的域名(保留https:// 和:636)。然后,您将在当前目录中有一个名为 certificate.cer 的文件,您可以打开并检查该文件。如果它不受信任,它会警告您。如果它不受信任,那么您必须在服务器上安装根证书作为受信任的根证书。

    如果它已受信任,请确保证书上的“颁发给:”域名与您用于连接的名称匹配。在我们的环境中,SSL 证书位于每个域控制器的名称中 (dc1.example.com),而不是域名 (example.com)。所以我必须针对 LDAPS 工作的特定域控制器。

    一旦你把所有这些都整理好,你的代码应该可以工作了。

    如果您想直接更改unicodePwd 属性而不是使用SetPassword(可能会或可能不会执行得更快),您需要通过SSL 建立原始连接。例如:

    DirectoryEntry de = new DirectoryEntry("LDAP://dc1.example.com:636","<username>", "<password>", 
        AuthenticationTypes.Secure | AuthenticationTypes.SecureSocketsLayer | AuthenticationTypes.ServerBind);
    

    如果您针对特定 DC,请仅使用 AuthenticationTypes.ServerBind。

    然后您可以按照它想要的非常具体的方式更新unicodePwd 属性:

    uEntry.Properties["unicodePwd"].Value = Encoding.Unicode.GetBytes("\"NewPassword\"");
    uEntry.CommitChanges();
    

    请注意,新密码必须用引号括起来。

    【讨论】:

    • 这对我有用(更改为使用 ServerName 而不是 IP)。谢谢。
    猜你喜欢
    • 2013-05-30
    • 1970-01-01
    • 2014-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    相关资源
    最近更新 更多