连接到 LDAP 服务器会引发 NullReferenceException

Posted

技术标签:

【中文标题】连接到 LDAP 服务器会引发 NullReferenceException【英文标题】:Connecting to LDAP server throws NullReferenceException 【发布时间】:2018-01-04 11:08:21 【问题描述】:

我正在尝试使用System.DirectoryServices.AccountManagement 连接到指定here 的在线测试LDAP 服务器,如下所示:

try

    using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "ldap.forumsys.com:389", "dc=example,dc=com", "cn=read-only-admin,dc=example,dc=com", "password"))
    
         using (var searcher = new PrincipalSearcher(new UserPrincipal(ctx )))
         
              foreach (var result in searcher.FindAll().Take(usersCount))
              
                 DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;
              
        
    

catch(Exception ex)

    Console.WriteLine(ex.Message);

但它会抛出以下异常:

对象引用未设置为对象的实例。

您能否告诉我的代码有什么问题以及如何能够连接到该 LDAP 服务器?

PS:我可以使用 Apache Directory Studio 连接到该服务器

堆栈跟踪:

在 System.DirectoryServices.AccountManagement.PrincipalContext.ReadServerConfig(字符串 serverName、ServerProperties& 属性) 在 System.DirectoryServices.AccountManagement.PrincipalContext.DoServerVerifyAndPropRetrieval() 在 System.DirectoryServices.AccountManagement.PrincipalContext..ctor(ContextType contextType,字符串名称,字符串容器,ContextOptions 选项,字符串用户名,字符串密码) 在 System.DirectoryServices.AccountManagement.PrincipalContext..ctor(ContextType contextType,字符串名称,字符串容器,字符串用户名,字符串密码) 在 C:\Users\Simple Code\source\repos\ConsoleApp1\ConsoleApp1\Program.cs:line 48 中的 ConsoleApp1.Program.GetGroups(String userName) 处

【问题讨论】:

What is a NullPointerException, and how do I fix it?的可能重复 简单来说,它无法使用您提供的 principalcontext 创建 ctx @Fildor 你搞混了。 NullPointerException => java, NullReferenceException => .NET What is a NullReferenceException, and how do I fix it?的可能重复 @SimpleCode 似乎(尽管来自 .NET Core 问题)如果找不到域,它可能会抛出 NRE。你能发布完整的堆栈跟踪吗? 【参考方案1】:

正如here 所说,问题可能是您尝试使用不支持此 OpenLDAP 的 PrincipalContext 类连接到 Apache Directory Studio

所以一种方法是使用DirectoryEntry

【讨论】:

DirectoryEntry entry = new DirectoryEntry("ldap.forumsys.com:389", "cn=gauss,ou=mathematicians,dc=example,dc=com", "password"); DirectorySearcher searcher = new DirectorySearcher(entry) PageSize = int.MaxValue, Filter = "(&(objectClass=user)(objectCategory=person))" ; searcher.PropertiesToLoad.Add("邮件"); var result1 = searcher.FindOne(); 它在 FindOne() 上抛出“未知错误 (0x80005000)” 这样不行,check 非常感谢。感谢您帮助我找出导致该异常的原因 @SimpleCode 仍然出现该异常?【参考方案2】:

使用 DirectoryEntry 它对我有用,如下所示:

using (var searcher = new DirectorySearcher(new DirectoryEntry("LDAP://ldap.forumsys.com:389/dc=example,dc=com", "", "", AuthenticationTypes.None)))

    searcher.Filter = "((objectClass=person))";
    searcher.PropertiesToLoad.Add("mail");//email
    searcher.PropertiesToLoad.Add("givenName");//first name
    searcher.PropertiesToLoad.Add("sn"); //last name
    searcher.PropertiesToLoad.Add("telephoneNumber");
    searcher.PropertiesToLoad.Add("description");
    searcher.PropertiesToLoad.Add("memberOf"); // groups

    var activeDirectoryStaffs = searcher.FindAll();
    if (activeDirectoryStaffs != null)
    
        for (int i = 0; i < activeDirectoryStaffs.Count; i++)
        
            SearchResult result = activeDirectoryStaffs[i];
            var Email = result.Properties.Contains("mail") ? (string)result.Properties["mail"][0]:null;
            var Mobile = result.Properties.Contains("telephoneNumber") ? (string)result.Properties["telephoneNumber"][0] : null;
            var FirstName = result.Properties.Contains("givenName") ? (string)result.Properties["givenName"][0] : null;
            var LastName = result.Properties.Contains("sn") ? (string)result.Properties["sn"][0] : null;
            var Description = result.Properties.Contains("description") ? (string)result.Properties["description"][0] : null;

        
    

【讨论】:

以上是关于连接到 LDAP 服务器会引发 NullReferenceException的主要内容,如果未能解决你的问题,请参考以下文章

如何在没有 DAO 的情况下使用 Spring LDAP 连接到多个 url?

无法从 python-ldap 连接到 Windows Server 2016 上的 ldaps

Android studio - 无法连接到LDAP服务器

使用 ADO 连接到 Sun LDAP

Smack 连接到 Google 的 XMPP 服务 (gtalk) 会引发异常“SASLError using PLAIN: not-authorized”

使用连接到 LDAP 服务器的生物识别系统验证窗口用户