在没有用户名和密码的情况下,从C#.NET 4.5.1检查LDAP服务器连接

在没有用户名和密码的情况下,从C#.NET 4.5.1检查LDAP服务器连接,c#,asp.net-web-api,ldap,C#,Asp.net Web Api,Ldap,任务是创建一个简单的WebAPI应用程序来测试与LDAP服务器的连接。我使用的是C#&.NET4.5.1。WebAPI应用程序在我们的DMZ中(不在任何域上) 我创建了一个简单的DirectorySearcher查找,它可以工作,但需要用户名和密码 在没有用户名和密码的情况下,该应用程序如何检查对LDAP服务器的访问 这是我的最新代码。我试图使用无效的用户名和密码登录。如果出现“Login Failed”(登录失败)消息,则我知道我已连接LDAP服务器。看起来像是个黑客。我想要一个更好的解决方案

任务是创建一个简单的WebAPI应用程序来测试与LDAP服务器的连接。我使用的是C#&.NET4.5.1。WebAPI应用程序在我们的DMZ中(不在任何域上)

我创建了一个简单的DirectorySearcher查找,它可以工作,但需要用户名和密码

在没有用户名和密码的情况下,该应用程序如何检查对LDAP服务器的访问

这是我的最新代码。我试图使用无效的用户名和密码登录。如果出现“Login Failed”(登录失败)消息,则我知道我已连接LDAP服务器。看起来像是个黑客。我想要一个更好的解决方案

    public Task<HttpResponseMessage> Ldap()
    {
        const string user = "0000";
        const string pass = "0000";
        const string messageTemplate = "{0} {1} {2} | LDAP: {3}";

        HttpResponseMessage response;
        var userName = String.Format("CN={0},OU=Users,DC=somelocation,DC=Org", user);
        var path = "LDAP://ldap.somelocation.org/" + userName;

        try
        {
            var directorySearcher = new DirectorySearcher
            {
                SearchRoot = new DirectoryEntry(path, userName, pass, AuthenticationTypes.ReadonlyServer),
                SearchScope = SearchScope.Subtree
            };
            directorySearcher.PropertiesToLoad.AddRange(new[]
            {
              "givenName"
            });
            var entry = directorySearcher.FindOne().GetDirectoryEntry();
            if (entry.Properties.Count > 0 && entry.Properties["givenName"].Value != null)
            {
                response = new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent(messageTemplate 
                        .FormatWith(ApiFriendlyName, BuildLabel, BuildNumber,
                            entry.Properties["givenName"].Value))
                };
            }
            else
            {
                response = new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent(messageTemplate 
                        .FormatWith(ApiFriendlyName, BuildLabel, BuildNumber, "User not found."))
                };
            }
        }
        catch (Exception ex)
        {
            response = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(messageTemplate 
                    .FormatWith(ApiFriendlyName, BuildLabel, BuildNumber, 
                        ex.Message.Contains("Logon failure")
                        ? "Connection made"
                        : "Connection failure"))
            };
        }
        return Task.FromResult(response);
    }
公共任务Ldap() { 常量字符串user=“0000”; 常量字符串pass=“0000”; const string messageTemplate=“{0}{1}{2}| LDAP:{3}”; HttpResponseMessage响应; var userName=String.Format(“CN={0},OU=Users,DC=somelocation,DC=Org”,user); var path=“LDAP://LDAP.somelocation.org/”+用户名; 尝试 { var directorySearcher=新的directorySearcher { SearchRoot=newdirectoryEntry(路径、用户名、密码、AuthenticationTypes.ReadonlyServer), SearchScope=SearchScope.Subtree }; directorySearcher.PropertiesToLoad.AddRange(新[] { “吉文纳姆” }); var entry=directorySearcher.FindOne().GetDirectoryEntry(); if(entry.Properties.Count>0&&entry.Properties[“givenName”].Value!=null) { response=新的HttpResponseMessage(HttpStatusCode.OK) { 内容=新的StringContent(messageTemplate .FormatWith(ApiFriendlyName、BuildLabel、BuildNumber、, entry.Properties[“givenName”].Value)) }; } 其他的 { response=新的HttpResponseMessage(HttpStatusCode.OK) { 内容=新的StringContent(messageTemplate .FormatWith(ApiFriendlyName、BuildLabel、BuildNumber,“未找到用户”)) }; } } 捕获(例外情况除外) { response=新的HttpResponseMessage(HttpStatusCode.OK) { 内容=新的StringContent(messageTemplate .FormatWith(ApiFriendlyName、BuildLabel、BuildNumber、, 例如消息包含(“登录失败”) ?“建立连接” :“连接失败”)) }; } 返回Task.FromResult(响应); }
您是否熟悉
PrincipalContext
您自己试过什么。。此外,这不是代码工厂站点。。请给我们看看你到目前为止都做了些什么……谢谢。我开始使用PrincipalContext,但后来又回到了这种方法,因为我不知道在没有用户的情况下如何实现我的目标。