首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UserPrincipal扩展返回计算机

UserPrincipal扩展返回计算机
EN

Stack Overflow用户
提问于 2018-08-30 14:11:53
回答 2查看 291关注 0票数 2

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

这个库是在.NetStandard 2.0中,我使用的是

代码语言:javascript
复制
System.DirectoryServices.AccountManagement

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

下面是我的类:

代码语言:javascript
复制
[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中进行搜索时,它按预期工作:

代码语言:javascript
复制
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
*/

但是如果我尝试使用我自己的类执行相同的搜索,结果也包含计算机:

代码语言:javascript
复制
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类具有以下属性:

代码语言:javascript
复制
[DirectoryObjectClass("user")]

我认为这足以在active directory中过滤此对象类型,但似乎没有。

知道这是怎么回事吗?

干杯

EN

回答 2

Stack Overflow用户

发布于 2019-01-27 18:31:05

microsoft Principal types filter creating code

同样面对这个在源代码中翻找的problem.Having,我找到了这样的变通方法。

代码语言:javascript
复制
[DirectoryObjectClass("user)(objectCategory=user")]
票数 1
EN

Stack Overflow用户

发布于 2019-10-05 17:02:51

在构造函数中,可以将ObjectCategory属性设置为User

代码语言:javascript
复制
[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); }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52090273

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档