gpt4 book ai didi

c# - 如何通过公用名更快地检索 Active Directory 用户?

转载 作者:IT王子 更新时间:2023-10-29 04:23:03 25 4
gpt4 key购买 nike

我正在查询来自 Active Directory 的信息.我有可用的代码,但它真的很慢。

这是我目前使用的代码:

    static void Main(string[] args)
{
SearchResultCollection sResults = null;

try
{
//modify this line to include your domain name
string path = "LDAP://EXTECH";
//init a directory entry
DirectoryEntry dEntry = new DirectoryEntry(path);

//init a directory searcher
DirectorySearcher dSearcher = new DirectorySearcher(dEntry);

//This line applies a filter to the search specifying a username to search for
//modify this line to specify a user name. if you want to search for all
//users who start with k - set SearchString to "k"
dSearcher.Filter = "(&(objectClass=user))";

//perform search on active directory
sResults = dSearcher.FindAll();

//loop through results of search
foreach (SearchResult searchResult in sResults)
{
if (searchResult.Properties["CN"][0].ToString() == "Adit")
{
////loop through the ad properties
//foreach (string propertyKey in
//searchResult.Properties["st"])
//{

//pull the collection of objects with this key name
ResultPropertyValueCollection valueCollection =
searchResult.Properties["manager"];

foreach (Object propertyValue in valueCollection)
{

//loop through the values that have a specific name
//an example of a property that would have multiple
//collections for the same name would be memberof
//Console.WriteLine("Property Name: " + valueCollection..ToString());
Console.WriteLine("Property Value: " + (string)propertyValue.ToString());

//["sAMAccountName"][0].ToString();
}
//}
Console.WriteLine(" ");
}
}
}
catch (InvalidOperationException iOe)
{
//
}
catch (NotSupportedException nSe)
{
//
}
finally
{

// dispose of objects used
if (sResults != null)
sResults.Dispose();

}
Console.ReadLine();
}

从 AD 获取用户信息的更快代码是什么样的?

最佳答案

您可以调用UserPrincipal.FindByIdentitySystem.DirectoryServices.AccountManagement 中:

using System.DirectoryServices.AccountManagement;

using (var pc = new PrincipalContext(ContextType.Domain, "MyDomainName"))
{
var user = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, "MyDomainName\\" + userName);
}

关于c# - 如何通过公用名更快地检索 Active Directory 用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/456523/

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