gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 20:38:55 27 4
gpt4 key购买 nike

请帮我解决这个问题。

我正在尝试使用以下代码获取用户组。我通过 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com