首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >c# PrincipalSearcher条件不像

c# PrincipalSearcher条件不像
EN

Stack Overflow用户
提问于 2014-07-21 19:04:21
回答 2查看 627关注 0票数 0

这是否有可能为设置一个“逐例查询”主体的条件--不像,而不是,比如

类方法:

代码语言:javascript
复制
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.Name= "Mario";

这将返回所有名为"Mario“的用户。

这是否有可能创建一个条件,让所有未命名为"Mario“的用户?

这样的东西:

代码语言:javascript
复制
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.Name != "Mario";

所有的用户用的是"Mario“以外的其他名字。

EN

回答 2

Stack Overflow用户

发布于 2014-07-21 19:35:51

不,但是你可以得到所有的用户。然后使用linq过滤它们。

代码语言:javascript
复制
UserPrincipal qbeUser = new UserPrincipal(ctx);
PrincipalSearcher pSearch = new PrincipalSearcher(qbeUser);
PrincipalSearchResult<Principal> pResult = pSearch.FindAll();
var notMario = (from u in pResult
                where u.Name != "Mario"
                select u);

那取决于你想做什么

代码语言:javascript
复制
foreach (Principal p in notMario) {
    // Do Somthing
}
票数 1
EN

Stack Overflow用户

发布于 2014-07-22 13:24:57

这可以通过扩展UserPrincipal类来实现:

代码语言:javascript
复制
[DirectoryRdnPrefix("CN")]
[DirectoryObjectClass("Person")]
public class UserPrincipalEXT : UserPrincipal
{
    // Inplement the constructor using the base class constructor. 
    public UserPrincipalEXT(PrincipalContext context)
        : base(context)
    { }

    // Implement the constructor with initialization parameters.    
    public UserPrincipalEXT(PrincipalContext context,
                         string samAccountName,
                         string password,
                         bool enabled)
        : base(context, samAccountName, password, enabled)
    { }

    // Create the "employeeType" property with the "!" for NOT LIKE.    
    [DirectoryProperty("!employeeType")]
    public string NotLikeEmployeeType
    {
        get
        {
            if (ExtensionGet("!employeeType").Length != 1)
                return string.Empty;

            return (string)ExtensionGet("!employeeType")[0];
        }
        set { ExtensionSet("!employeeType", value); }
    }

    // Implement the overloaded search method FindByIdentity.
    public static new UserPrincipalEXT FindByIdentity(PrincipalContext context, string identityValue)
    {
        return (UserPrincipalEXT)FindByIdentityWithType(context, typeof(UserPrincipalEXT), identityValue);
    }

    // Implement the overloaded search method FindByIdentity. 
    public static new UserPrincipalEXT FindByIdentity(PrincipalContext context, IdentityType identityType, string identityValue)
    {
        return (UserPrincipalEXT)FindByIdentityWithType(context, typeof(UserPrincipalEXT), identityType, identityValue);
    }
}

重要的是要明白这一点:

代码语言:javascript
复制
// Create the "employeeType" property.    
[DirectoryProperty("!employeeType")]
public string NotLikeEmployeeType
{
    get
    {
        if (ExtensionGet("!employeeType").Length != 1)
                return string.Empty;

        return (string)ExtensionGet("!employeeType")[0];
     }
     set { ExtensionSet("!employeeType", value); }
}

假设带有ExtensionGet和ExtensionSet的“NotLikeEmployeeType”在属性(在本例中为NotLikeEmployeeType)不是空时创建条件,则可以添加"!“在AD属性之前(本例中为employeeType)。

以下是我们可以使用扩展的方式:

代码语言:javascript
复制
UserPrincipalEXT qbeUser = new UserPrincipalEXT(ctx);
qbeUser.NotLikeEmployeeType = "exclude";

现在返回的条件是:

代码语言:javascript
复制
!employeeType = exclude

这正是我们想要的!不像!)

而且,扩展的好处是可以将AD属性添加到通常不公开的类中(如employeeType)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24872715

复制
相关文章

相似问题

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