gpt4 book ai didi

c# - UserPrincipal 扩展返回计算机

转载 作者:行者123 更新时间:2023-12-05 04:04:45 24 4
gpt4 key购买 nike

我是事件目录的新手,我目前正在为一个项目开发一个库,以轻松管理我们的事件目录对象,如用户、资源、组等。

该库位于 .NetStandard 2.0 中,我使用的是 Principal 类

System.DirectoryServices.AccountManagement

由于 UserPrincipal 类不包含我们可能需要的所有属性,我尝试实现一个 UserPrincipalExtended 类,目前只添加 Initials 属性。

这是我的类(class):

[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 类在事件目录中进行搜索时,它按预期工作:

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")]

我认为这足以在事件目录中过滤此对象类型,但似乎没有。

知道这里发生了什么吗?

干杯

最佳答案

microsoft Principal types filter creating code

同样遇到了这个问题,翻遍了源码,找到了这样的解决方法。

[DirectoryObjectClass("user)(objectCategory=user")]

关于c# - UserPrincipal 扩展返回计算机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52090273/

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