Vinc 웃
Vinc 웃

Reputation: 1257

c# PrincipalSearcher Condition Not Like

Is this possible to set a condition for a "query-by-example" principal to NOT LIKE rather than LIKE ?

Like Method:

UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.Name= "Mario";

This will return all users with the name "Mario".

Is this possible to create a condition to get all the users who are not named "Mario" ?

Something like this :

UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.Name != "Mario";

All users with an other name than "Mario".

Upvotes: 0

Views: 660

Answers (2)

Vinc 웃
Vinc 웃

Reputation: 1257

This can be achieved by extending the UserPrincipal class :

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

The important thing to understand is here :

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

Given that the "DirectoryProperty" with ExtensionGet AND ExtensionSet create the condition when the property (NotLikeEmployeeType in this case) is not empty, you can add an "!" before the AD property (employeeType in this case).

This is how we can use the extension :

UserPrincipalEXT qbeUser = new UserPrincipalEXT(ctx);
qbeUser.NotLikeEmployeeType = "exclude";

Now the condition returned will be :

!employeeType = exclude

And this is exactly what we want ! Not Like ! :)

And, the great thing about extension, is you can add AD properties to the class that they are normally not exposed (like employeeType)

Upvotes: 1

ca.adamgordon
ca.adamgordon

Reputation: 17

No, however you can get all users. Then use linq to filter them.

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

Then depending on what you want to do

foreach (Principal p in notMario) {
    // Do Somthing
}

Upvotes: 1

Related Questions