我是active directory的新手,目前我正在为一个项目开发一个库,以便轻松地管理active directory对象,如用户、资源、组等。
这个库是在.NetStandard 2.0中,我使用的是
System.DirectoryServices.AccountManagement由于UserPrincipal类不包含我们需要的所有属性,因此我尝试实现一个UserPrincipalExtended类,目前只添加缩写属性。
下面是我的类:
[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$
*/因为我的UserPrincipalExtended类具有以下属性:
[DirectoryObjectClass("user")]我认为这足以在active directory中过滤此对象类型,但似乎没有。
知道这是怎么回事吗?
干杯
发布于 2019-01-27 18:31:05
microsoft Principal types filter creating code
同样面对这个在源代码中翻找的problem.Having,我找到了这样的变通方法。
[DirectoryObjectClass("user)(objectCategory=user")]发布于 2019-10-05 17:02:51
在构造函数中,可以将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); }
}
}https://stackoverflow.com/questions/52090273
复制相似问题