C# 如何克服Active Directory搜索中的后期绑定
我有一个基于用户名和域检索用户全名的函数。此函数在ASP.NET线程中模拟用户下运行。 当我在远程AD分支上使用Directory searcher时,我相信我得到的是SID号而不是属性(无法验证它是否出现在其他框中)C# 如何克服Active Directory搜索中的后期绑定,c#,active-directory,C#,Active Directory,我有一个基于用户名和域检索用户全名的函数。此函数在ASP.NET线程中模拟用户下运行。 当我在远程AD分支上使用Directory searcher时,我相信我得到的是SID号而不是属性(无法验证它是否出现在其他框中) 我已经将广告包装到一个方便的助手库中,并一直使用此方法: /// <summary> /// Returns AD information for a specified userID. /// </summary> ///
我已经将广告包装到一个方便的助手库中,并一直使用此方法:
/// <summary>
/// Returns AD information for a specified userID.
/// </summary>
/// <param name="ntID"></param>
/// <returns></returns>
public ADUser GetUser(string ntID)
{
DirectorySearcher search = new DirectorySearcher();
search.Filter = String.Format("(cn={0})", ntID);
search.PropertiesToLoad.Add("mail");
search.PropertiesToLoad.Add("givenName");
search.PropertiesToLoad.Add("sn");
search.PropertiesToLoad.Add("displayName");
search.PropertiesToLoad.Add("userPrincipalName");
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();
return new ADUser(result);
}
//
///返回指定用户ID的广告信息。
///
///
///
公共ADUser GetUser(字符串ntID)
{
DirectorySearcher search=新建DirectorySearcher();
search.Filter=String.Format(“(cn={0})”,ntID);
search.PropertiesToLoad.Add(“邮件”);
search.PropertiesToLoad.Add(“givenName”);
search.PropertiesToLoad.Add(“序号”);
search.PropertiesToLoad.Add(“displayName”);
search.PropertiesToLoad.Add(“userPrincipalName”);
search.PropertiesToLoad.Add(“cn”);
SearchResult=search.FindOne();
返回新的ADUser(结果);
}
ADUser是一个自定义类,它将搜索结果映射到强类型属性
我不确定你的具体问题是什么,但这一直对我有效
编辑:比较我们的代码,我发现您没有告诉search预加载属性…这可能是您的问题。您使用的是哪个.NET framework版本?在.NET3.5中,广告内容已经进行了相当广泛的修改,现在为用户和组等提供了强类型结构 查看我的好友乔·卡普兰和伊桑·威兰斯基在MSDN上的精彩文章“”。真是太棒了 首先,您得到一个名为UserPrincipal的强类型类,例如,所有基本属性都是对象上的属性。确实很有帮助 其次,您可以使用PrincipalSearcher获得一个很好的“示例查询”方法—查看Joe和Ethan文章中的示例:
// create a principal object representation to describe
// what will be searched
UserPrincipal user = new UserPrincipal(adPrincipalContext);
// define the properties of the search (this can use wildcards)
user.Enabled = false;
user.Name = "user*";
// create a principal searcher for running a search operation
PrincipalSearcher pS = new PrincipalSearcher();
// assign the query filter property for the principal object
// you created
// you can also pass the user principal in the
// PrincipalSearcher constructor
pS.QueryFilter = user;
// run the query
PrincipalSearchResult<Principal> results = pS.FindAll();
Console.WriteLine("Disabled accounts starting with a name of 'user':");
foreach (Principal result in results)
{
Console.WriteLine("name: {0}", result.Name);
}
//创建一个主要对象表示来描述
//将搜索什么
UserPrincipal用户=新的UserPrincipal(adPrincipalContext);
//定义搜索的属性(可以使用通配符)
user.Enabled=false;
user.Name=“user*”;
//创建用于运行搜索操作的主体搜索器
PrincipalSearcher pS=新PrincipalSearcher();
//为主体对象指定查询筛选器属性
//你创造了
//您还可以在中传递用户主体
//主搜索构造函数
pS.QueryFilter=用户;
//运行查询
PrincipalSearchResult结果=pS.FindAll();
Console.WriteLine(“以“用户”名称开头的已禁用帐户:”;
foreach(结果中的主要结果)
{
WriteLine(“名称:{0}”,result.name);
}
如果有机会的话,试着访问.NET3.5获取你的广告资料
MarcHi FlySwat,DirectorySearcher构造函数具有加载数组的属性——在我的例子中是{“displayName”}。我会尝试使用ADUser类。谢谢,ADUser是我写的一个类,它让与用户打交道更加理智。
// create a principal object representation to describe
// what will be searched
UserPrincipal user = new UserPrincipal(adPrincipalContext);
// define the properties of the search (this can use wildcards)
user.Enabled = false;
user.Name = "user*";
// create a principal searcher for running a search operation
PrincipalSearcher pS = new PrincipalSearcher();
// assign the query filter property for the principal object
// you created
// you can also pass the user principal in the
// PrincipalSearcher constructor
pS.QueryFilter = user;
// run the query
PrincipalSearchResult<Principal> results = pS.FindAll();
Console.WriteLine("Disabled accounts starting with a name of 'user':");
foreach (Principal result in results)
{
Console.WriteLine("name: {0}", result.Name);
}