gpt4 book ai didi

c# - 使用 C# 获取事件目录用户数据

转载 作者:行者123 更新时间:2023-11-30 21:28:53 25 4
gpt4 key购买 nike

我想获取有关特定用户的数据。我知道此用户的 OU 路径,但无法使用该 OU 路径获取信息。它总是说找不到用户。谁能告诉我是否需要更改搜索过滤器。请帮忙 。

代码

用户路径abc.ds.xyz.net/fGroup/xcxc/Users/123456abc.ds.xyz.net 是域然后 fGroup 是 OU , xcxc 是 OU , Users 是 OU , 123456 是 cn 。

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
using System.Web.UI.WebControls;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace Ldap_authentication
{
public class Program
{
static void Main(string[] args)
{

Console.Write("Enter user: ");
String username = Console.ReadLine();

try
{
DirectoryEntry myLdapConnection = createDirectoryEntry();

DirectorySearcher search = new DirectorySearcher(myLdapConnection);
search.Filter = "(&(OU=fGroup)(OU=xcxc )(OU=Users)(cn=" + username + "))";
SearchResult result = search.FindOne();

if (result != null)
{

ResultPropertyCollection fields = result.Properties;

foreach (String ldapField in fields.PropertyNames)
{

foreach (Object myCollection in fields[ldapField])
Console.WriteLine(String.Format("{0,-20} : {1}",
ldapField, myCollection.ToString ()));
}
}

else
{
// user does not exist
Console.WriteLine("User not found!");
Console.ReadLine();
}
}

catch (Exception e)
{
Console.WriteLine("Exception caught:\n\n" + e.ToString());
Console.ReadLine();
}


}
static DirectoryEntry createDirectoryEntry()
{
DirectoryEntry ldapConnection = new DirectoryEntry("abc.ds.xyz.net");
ldapConnection.Path = "LDAP://DC=abc,DC=ds,DC=xyz,DC=net";
ldapConnection.AuthenticationType = AuthenticationTypes.Secure;
return ldapConnection;
}
}
}

编辑

    search.Filter = "(&(OU=fGroup))";
SearchResult result = search.FindOne();

当我更改 search.Filter = "(&(OU=fGroup)(OU=xcxc )(OU=Users)(cn="+ username + "))";进入 search.Filter = "(&(OU=fGroup))";我得到结果。谁能告诉我如何使用多个搜索输入过滤器进行搜索。

最佳答案

在用头撞墙数小时后,我终于找到了答案。我需要编写多个搜索过滤器,例如

旧代码 search.Filter = "(&(OU=fGroup))";用这个替换这个

   search.Filter = "(&(OU=fGroup))";
search.Filter = "(&(OU=xcxc))";
search.Filter = "(&(OU=Users))";
search.Filter = "(&(cn=" + username + "))";
SearchResult result = search.FindOne();

Finally found my answer :) . Happy Coding guys :)

关于c# - 使用 C# 获取事件目录用户数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55951799/

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