LDAP reset password from outside the domain network C# Error: RPC server is unavailable. (exception from hresult: 0x800706ba)
LDAP reset password from outside the domain network C# Error: RPC server is unavailable. (exception from hresult: 0x800706ba)
我们正在尝试重置 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", ...)
实际上是调用原生WindowsIADsUser::SetPassword
方法。该文档显示它会自动尝试几种不同的加密方式。发生异常是因为 none 这些方法有效。
你实际上可以直接修改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
的文件,您可以打开并检查它。如果它不受信任,它会警告您。如果它不受信任,那么您将必须在服务器上安装根证书作为受信任的根证书。
如果它已被信任,请确保证书上的 "Issued to:" 域名与您用于连接的名称匹配。在我们的环境中,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();
注意新密码必须用引号引起来。
我们正在尝试重置 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", ...)
实际上是调用原生WindowsIADsUser::SetPassword
方法。该文档显示它会自动尝试几种不同的加密方式。发生异常是因为 none 这些方法有效。
你实际上可以直接修改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
的文件,您可以打开并检查它。如果它不受信任,它会警告您。如果它不受信任,那么您将必须在服务器上安装根证书作为受信任的根证书。
如果它已被信任,请确保证书上的 "Issued to:" 域名与您用于连接的名称匹配。在我们的环境中,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();
注意新密码必须用引号引起来。