[英]Connect to LDAP server and hit error in ASP.NET C# webform
I am using Windows authentication in a Webforms application, and I want to get the user's email address, but I think I hit the error when connecting to the server.我在 Webforms 应用程序中使用 Windows 身份验证,我想获取用户的电子邮件地址,但我认为我在连接到服务器时遇到了错误。 Anything wrong with my code?
我的代码有什么问题吗?
I had tried the strAccountId
with/without domain name, (sAMAccountName=john)
.我试过带/不带域名的
strAccountId
(sAMAccountName=john)
。
The server is not operational.
服务器无法运行。
Description: An unhandled exception occurred during the execution of the current web request.
说明:执行当前 Web 请求期间发生未处理的异常。 Please review the stack trace for more information about the error and where it originated in the code.
请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
Exception Details: System.Runtime.InteropServices.COMException: The server is not operational
异常详细信息:System.Runtime.InteropServices.COMException:服务器无法运行
Code:代码:
string path = "LDAP://XYZ.LOCAL/CN=XYZ.LOCAL,OU=XXX,DC=XYZ,DC=LOCAL";
// The value of User.Identity.Name is XYZ\john
string strAccountId = "XYZ\\john";
string strPassword = "xxxxx";
bool bSucceeded;
string strError;
DirectoryEntry adsEntry = new DirectoryEntry(path, strAccountId, strPassword);
DirectorySearcher adsSearcher = new DirectorySearcher(adsEntry);
adsSearcher.Filter = "(sAMAccountName=" + strAccountId + ")";
try
{
SearchResult adsSearchResult = adsSearcher.FindOne();
bSucceeded = true;
strError = "User has been authenticated by Active Directory.";
EmailMsg.Text = strError;
adsEntry.Close();
}
catch (Exception ex)
{
bSucceeded = false;
strError = ex.Message;
EmailMsg.Text = strError;
adsEntry.Close();
}
In path you cannot put OUs, you need to do that after with adsEntry.Path.在路径中你不能放置 OU,你需要在 adsEntry.Path 之后这样做。
string path = "LDAP://XYZ.LOCAL";
string strAccountId = "XYZ.LOCAL\\john";
string strPassword = "xxxxx";
DirectoryEntry adsEntry = new DirectoryEntry(path, strAccountId, strPassword);
adsEntry.Path = "LDAP://CN=XYZ.LOCAL,OU=XXX,DC=XYZ,DC=LOCAL";
Your path has three parts:您的路径包含三个部分:
LDAP://
is the protocol LDAP://
是协议XYZ.LOCAL
is the server to connect to. XYZ.LOCAL
是要连接的服务器。 This is optional and can be excluded if the computer you run this from is joined to the same domain you're trying to connect to, or to a trusted domain.CN=XYZ.LOCAL,OU=XXX,DC=XYZ,DC=LOCAL
is the object on the domain to bind to. CN=XYZ.LOCAL,OU=XXX,DC=XYZ,DC=LOCAL
是域上要绑定的对象。 This is also optional. Since you have included the optional server name, it will try to connect to XYZ.LOCAL
on the default LDAP port 389. "The server is not operational" means that it could not open a connection to XYZ.LOCAL
on port 389. This is a network error and you need to figure out why the domain is not accessible from the computer you are running this from.由于您已包含可选的服务器名称,它将尝试在默认 LDAP 端口 389 上连接到
XYZ.LOCAL
。“服务器无法运行”意味着它无法在端口 389 上打开与XYZ.LOCAL
的连接。这是网络错误,您需要弄清楚为什么无法从您运行它的计算机访问域。
You can test the connection in PowerShell using:您可以使用以下命令在 PowerShell 中测试连接:
Test-NetConnection XYZ.LOCAL -Port 389
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.