Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法通过LDAP连接_C#_Asp.net_Active Directory_Ldap_Asp.net Web Api2 - Fatal编程技术网

C# 无法通过LDAP连接

C# 无法通过LDAP连接,c#,asp.net,active-directory,ldap,asp.net-web-api2,C#,Asp.net,Active Directory,Ldap,Asp.net Web Api2,我正在使用ASP.NET技术进行WEB API项目。此Web API需要使用LDEP:// [HttpGet] public IHttpActionResult ListProperties(string domainName, string userName, string password) { try { using (DirectoryEntry dEntry = new DirectoryEntry("LD

我正在使用ASP.NET技术进行WEB API项目。此Web API需要使用
LDEP://

    [HttpGet]
    public IHttpActionResult ListProperties(string domainName, string userName, string password)
    {
        try
        {
            using (DirectoryEntry dEntry = new DirectoryEntry("LDAP://" + domainName, userName, password))
            {
                DirectorySearcher dSearcher = new DirectorySearcher(dEntry)
                {
                    Filter = "(&(objectClass=user)(mail=" + userName + "))"
                };
                SearchResult sResult = dSearcher.FindOne();
                Dictionary<string, string> resultDictionary = new Dictionary<string, string>
                {
                    {"Name", GetProperty(sResult,"cn")},
                    {"Email", GetProperty(sResult,"mail")}
                };

                return Ok(resultDictionary.ToList());
            }
        }
        catch (Exception ex)
        {
            return BadRequest(ex.Message);
        }
    }


    private string GetProperty(SearchResult searchResult, string propertyName)
    {
        if (searchResult.Properties.Contains(propertyName))
        {
            return searchResult.Properties[propertyName][0].ToString();
        }
        return string.Empty;
    }
不幸的是,我总是收到错误的请求或下面的错误

System.Runtime.InteropServices.COMException HResult=0x8007203A Message=The server is not operational.

您能为我提供一个如何解决此问题的指南吗,因为我以前从未使用过安全编程。

此错误意味着无法从域控制器获得任何响应。可能是与域控制器没有网络连接

要测试网络连接,您可以在PowerShell中使用
domainName
变量代替“domain.com”进行测试

没有输出意味着它成功了。如果失败,它会告诉你一条红色的错误消息

如果这不起作用,请尝试其他方法之一。AD LDAP可以在4个端口中的任意一个上工作:

  • 389:LDAP-读取/写入单个域-如果不指定端口,这是默认值
  • 636:SSL上的LDAP-与389相同,但已加密
  • 3268:全局目录-对您的广告林只读(如果您有多个域,否则它与389没有区别,只读除外)
  • 3269:通过SSL的全局编录
如果其他端口中有一个正常工作,您可以在代码中指定它,如下所示:

new DirectoryEntry("LDAP://" + domainName + ":636", userName, password)
对于端口3268,您还可以使用“GC://”,而不是指定端口:

新目录条目(“GC://”+域名、用户名、密码)


如果这些端口都不起作用,那么在继续之前,您需要确定您与域的网络连接。

您可以删除第
行的用户名、密码和新目录条目(“LDAP://“+domainName,username,password”)
吗?
(New-Object Net.Sockets.TcpClient).Connect("domain.com", 389)
new DirectoryEntry("LDAP://" + domainName + ":636", userName, password)