帮我解决这个问题。

我正在尝试使用以下代码获取用户组。我遍历了 mono 。正常获取的OS Windows数据(该帐户不包含在域中)。但是,当我在Linux上启动相同的代码时,会收到错误消息。

我需要怎么做才能获得正常结果?

using System;
using System.Text;
using System.DirectoryServices;
using System.Runtime.InteropServices;

namespace ActiveDirectoryTest
{
    class Program
    {
        private static void Main(string[] args)
        {
            try
            {
                DirectoryEntry de = new DirectoryEntry("LDAP://sub.domain.com","username@domain","password",AuthenticationTypes.None);

                DirectorySearcher search = new DirectorySearcher(de);
                search.ReferralChasing=ReferralChasingOption.All;
                search.Filter = "(&(ObjectClass=user)(sAMAccountName=username))";

                search.PropertiesToLoad.Add("sAMAccountName");
                search.PropertiesToLoad.Add("memberOf");
                StringBuilder groupNames = new StringBuilder();

                var result = search.FindAll()[0];
                int propertyCount = result.Properties["memberOf"].Count;

                for (int propertyCounter = 0;
                    propertyCounter < propertyCount;
                    propertyCounter++)
                {
                    var dn = (String) result.Properties["memberOf"][propertyCounter];

                    var equalsIndex = dn.IndexOf("=", 1);
                    var commaIndex = dn.IndexOf(",", 1);
                    if (-1 == equalsIndex)
                    {
                        Console.WriteLine("error parse");
                    }
                    groupNames.Append(dn.Substring((equalsIndex + 1),
                        (commaIndex - equalsIndex) - 1));
                    groupNames.Append("|");
                }

                Console.WriteLine(groupNames.ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            Console.ReadLine();
        }
    }
}

最佳答案

当搜索库无效时,通常会产生此错误。当您使用明文LDAP(我的以下示例使用SSL,但是您可以注释掉将身份验证类型更改为System.DirectoryServices.AuthenticationTypes.None)时,可以在以下位置捕获应用程序主机和LDAP服务器之间的网络捕获:端口389,并查看正在执行的实际搜索。

对于MS's documentation,您应该能够使用LDAP://dc = company,dc = gTLD而无需指定特定的域 Controller 。因为我需要我的代码可同时用于Active Directory和纯LDAP服务器,所以我使用LDAP://DomainController.company.gTLD/ou=UserOU,dc=company,dc=gTLD之类的名称,其中LDAP主机名和搜索基是包括。

我用于LDAP身份验证的功能:

protected string ldapAuthentication(string strLDAPServer, string strSuppliedUser, string strSuppliedPwd, string strSystemUID, string strSystemPwd, string strLDAPUserBase, string strUIDAttr){
    strSuppliedUser = strSuppliedUser.Trim();
string strResults = "";
    string strLDAPUserHost = strLDAPServer + strLDAPUserBase;

    // Establish LDAP connection and bind with system ID
    System.DirectoryServices.DirectoryEntry dirEntry = new System.DirectoryServices.DirectoryEntry();
    dirEntry.Path = strLDAPUserHost;
    dirEntry.Username = strSystemUID;
    dirEntry.Password = strSystemPwd;

dirEntry.AuthenticationType = System.DirectoryServices.AuthenticationTypes.SecureSocketsLayer;

    try
    {
        dirEntry.RefreshCache();

        // Search directory for the user logging on
        string strLDAPFilter = "(&(objectClass=user)(" + strUIDAttr + "=" + strSuppliedUser + "))";
        System.DirectoryServices.DirectorySearcher ldapSearch = new System.DirectoryServices.DirectorySearcher(dirEntry);
        ldapSearch.ServerTimeLimit = new TimeSpan(0, 0, 30);


        ldapSearch.Filter = strLDAPFilter;
        ldapSearch.SearchScope = System.DirectoryServices.SearchScope.Subtree;

        System.DirectoryServices.SearchResultCollection searchResults = ldapSearch.FindAll();


        if (searchResults.Count == 1){
        ...

该函数的调用方式如下:
strInputResults = ldapAuthentication("LDAP://DomainController.company.gTLD/", strInputSuppliedUser, strInputSuppliedPwd, "[email protected]", "Syst3mP@s5w0rd", "ou=UserOU,dc=company,dc=gTLD","sAMAccountName");

关于c# - 从Active Directory获取用户组时出错,在Mono中使用LDAP,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32584772/

10-16 20:09