我们正在尝试重置 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)
最佳答案
用于修改密码的属性为unicodePwd
.该文档揭示了更改密码必须满足的一些条件。首先,连接必须加密。
调用 .Invoke("SetPassword", ...)
实际上调用了 native Windows IADsUser::SetPassword
方法。该文档显示它会自动尝试几种不同的加密方式。发生异常是因为这些方法均无效。
您实际上可以直接修改unicodePwd
属性,而无需调用SetPassword
,我会讲到,但无论如何,您必须先解决加密问题。
当您从网络内的计算机运行它时,AuthenticationTypes.Sealing
就足够了。作为the documentation says ,效果是它使用 Kerberos 来加密连接。
但是当您从域外进行连接时,Kerberos 将无法工作(也许它会努力 - 我不是 Kerberos 专家)。所以唯一可用的加密方法是 SSL。 SetPassword
方法实际上尝试使用 SSL,但显然它不起作用。
我马上看到的一个问题是您正在使用 IP 地址连接到 DC,而 SSL 使用 IP 地址将无法工作,因为 SSL 证书上的域名名称必须与您的名称相匹配用于访问服务器,SSL 证书上将没有 IP 地址。因此,您必须更改它才能使用域名。如果 DNS 无法解析该名称,您可以将其添加到您的主机文件中。
改变可能解决一切。如果不是,可能还有其他两个问题:
- 端口 636 不可访问(防火墙挡住了路)。
- 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 证书位于每个域 Controller 的名称中 (dc1.example.com
),而不是域名 (example.com
)。因此,我必须针对 LDAPS 工作的特定域 Controller 。
一旦您解决了所有这些问题,您的代码就应该可以工作了。
如果您想直接更改 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();
请注意,新密码必须用引号括起来。
关于c# - 从域网络外部的 LDAP 重置密码 C# 错误 : RPC server is unavailable.(hresult 的异常:0x800706ba),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62087395/