繁体   English   中英

UserPrincipal 扩展返回计算机

[英]UserPrincipal extensions returns computers

我对活动目录还很陌生,我目前正在为一个项目开发一个库,以便轻松管理我们的活动目录对象,如用户、资源、组等。

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

System.DirectoryServices.AccountManagement

由于 UserPrincipal 类不包含我们可能需要的所有属性,我尝试实现一个 UserPrincipalExtended 类,现在只添加 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 类在活动目录中进行搜索时,它按预期工作:

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 主体类型过滤器创建代码

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

[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); }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM