简体   繁体   English

连接到 LDAP 服务器并在 ASP.NET C# webform 中遇到错误

[英]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:您的路径包含三个部分:

  1. LDAP:// is the protocol LDAP://是协议
  2. 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.这是可选的,如果您运行它的计算机加入了您尝试连接的同一个域或受信任的域,则可以将其排除在外。
  3. 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.这也是可选的。 If excluded, it will bind to the root of the domain that the server in part 2 is part of.如果排除,它将绑定到第 2 部分中的服务器所属的域的根。 You must include either part 2 or 3, or both.您必须包含第 2 部分或第 3 部分,或两者都包含。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM