c# - 从 Active Directory 获取用户组时出错,在单声道中使用 LDAP

标签 c# active-directory mono ldap

请帮我解决这个问题。

我正在尝试使用以下代码获取用户组。我通过 mono 运行。正常获取的操作系统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();
        }
    }
}

LdapException: (32) No Such Object LdapException: Server Message: 0000208D: NameErr: DSID-03100213, problem 2001 (NO_OBJECT), data 0, best match of: '' Novell.Directory.Ldap.LdapException

最佳答案

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

根据 MS's documentation ,您应该能够在不指定特定域 Controller 的情况下使用 LDAP://dc=company,dc=gTLD。因为我需要我的代码同时适用于 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, "SystemAccount@company.gTLD", "Syst3mP@s5w0rd", "ou=UserOU,dc=company,dc=gTLD","sAMAccountName");

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

相关文章:

C# 和 .NET : How to serialize a structure into a byte[] array, 使用 BinaryWriter?

java - 从 java 程序更改 Active Directory 用户密码

c# - 使用 C++ 和嵌入式单声道调用 C# DLL 时无法加载程序集系统

python - 如何在树莓派上运行可执行文件

c++ - 传递给 LogonUser() 的密码不正确,但 Active Directory 帐户未按预期锁定

linux - MonoDevelop 产生 "Error launching web browser"——如何解决这个问题?

c# - MSSQL参数消耗更多时间

c# - 如何正确捕获调用存储过程的返回值

c# - 在类型上强制执行静态方法

java - LDAP 服务器更新和事件通知