C# UserPrincipal扩展返回计算机
我对active directory非常陌生,目前正在为一个项目开发一个库,以便轻松管理我们的active directory对象,如用户、资源、组等 该库位于.netstandard2.0中,我使用C# UserPrincipal扩展返回计算机,c#,.net,active-directory,ldap,userprincipal,C#,.net,Active Directory,Ldap,Userprincipal,我对active directory非常陌生,目前正在为一个项目开发一个库,以便轻松管理我们的active directory对象,如用户、资源、组等 该库位于.netstandard2.0中,我使用 System.DirectoryServices.AccountManagement 由于UserPrincipal类不包含我们可能需要的所有属性,因此我尝试实现一个UserPrincipalExpended类,目前只添加Initials属性 这是我的班级: [DirectoryObjectCl
System.DirectoryServices.AccountManagement
由于UserPrincipal类不包含我们可能需要的所有属性,因此我尝试实现一个UserPrincipalExpended类,目前只添加Initials属性
这是我的班级:
[DirectoryObjectClass("user")]
[DirectoryRdnPrefix("CN")]
public class UserPrincipalExtended : UserPrincipal
{
public UserPrincipalExtended(PrincipalContext context) : base(context) { }
public UserPrincipalExtended(PrincipalContext context, string samAccountName, string password, bool enabled) : base(context, samAccountName, password, enabled) { }
[DirectoryProperty("Initials")]
public string Initials
{
get
{
if (ExtensionGet("initials").Length != 1) return null;
return (string)ExtensionGet("initials")[0];
}
set { ExtensionSet("initials", value); }
}
public static new UserPrincipalExtended FindByIdentity(PrincipalContext context, string identityValue)
{
return (UserPrincipalExtended)FindByIdentityWithType(context, typeof(UserPrincipalExtended), identityValue);
}
public static new UserPrincipalExtended FindByIdentity(PrincipalContext context, IdentityType identityType, string identityValue)
{
return (UserPrincipalExtended)FindByIdentityWithType(context, typeof(UserPrincipalExtended), identityType, identityValue);
}
}
当我使用UserPrincipal类在active directory中进行搜索时,它会按预期工作:
using (var context = _contextProvider.GetPrincipalContext())
using (var query = new UserPrincipal(context))
using (var searcher = new PrincipalSearcher(query))
{
foreach (var principal in searcher.FindAll())
{
UserPrincipal userPrincipal = principal as UserPrincipal;
if (CheckHelper.IsFilled(userPrincipal))
{
Console.WriteLine($"{userPrincipal.StructuralObjectClass} : {userPrincipal.SamAccountName}");
}
}
}
/*Output
user : cadmin
user : Guest
user : DefaultAccount
*/
但如果我尝试使用自己的类执行相同的搜索,则结果还包含计算机:
using (var context = _contextProvider.GetPrincipalContext())
using (var query = new UserPrincipalExtended(context))
using (var searcher = new PrincipalSearcher(query))
{
foreach (var principal in searcher.FindAll())
{
UserPrincipalExtended userPrincipalExtended = principal as UserPrincipalExtended;
if (CheckHelper.IsFilled(userPrincipalExtended))
{
Console.WriteLine($"userPrincipalExtended.StructuralObjectClass} : {userPrincipalExtended.SamAccountName}");
}
}
}
/*Output
user : cadmin
user : Guest
user : DefaultAccount
computer : WS001$
computer : WS002$
computer : WS003$
*/
由于我的UserPrincipalExtrade类具有以下属性:
[DirectoryObjectClass("user")]
我认为这足以在active directory中过滤此对象类型,但似乎不行
你知道这是怎么回事吗
干杯
也面临着这个问题。翻阅了源代码后,我找到了这样一个解决方法
[DirectoryObjectClass("user)(objectCategory=user")]
在构造函数中,可以将
ObjectCategory
属性设置为User
[DirectoryObjectClass("user")]
[DirectoryRdnPrefix("CN")]
public class UserPrincipalExtended : UserPrincipal
{
public UserPrincipalExtended(PrincipalContext context) : base(context)
{
// Set ObjectCategory to user so computer objects are not returned
ExtensionSet("objectCategory", "user");
}
[DirectoryProperty("Initials")]
public string Initials
{
get
{
if (ExtensionGet("initials").Length != 1) return null;
return (string)ExtensionGet("initials")[0];
}
set { ExtensionSet("initials", value); }
}
}
计算机
继承自标准AD目录架构中的用户
。所有计算机都是用户。但是,当我使用UserPrinicpal进行搜索时,为什么不返回它们呢?既然我扩展了这个类,为什么行为会有所不同呢?Microsoft没有发布相关的源代码,所以你必须使用反编译器来挖掘。在你的类中,你使用的是userprincipalex
。在示例中,您使用的是active directory中的搜索UserPrincipal
。因此,您没有尝试执行与您所述相同的搜索。