I am searching in Active Directory for specific SamAccountName
values using the PrincipalSearcher
object because I want to return UserPrincipal
's. I was wondering how I can apply two filters to that search; one being the beginning of the account name starts with xx and the other being it does not end with _c.
Currently I can search for all results beginning with xx by using xx*
but I cannot figure out how to add another search term or even apply a search term for does not equal. This is what I'm currently working with.
protected override void RunTests()
{
using (PrincipalContext context = new PrincipalContext(ContextType.Domain, "NAME", "OU=OUName",OU=name,DC=name,DC=net"))
{
UserPrincipal searchTemplate = new UserPrincipal(context);
searchTemplate.Enabled = true;
searchTemplate.SamAccountName = "xx*";
PrincipalSearcher search = new PrincipalSearcher(searchTemplate);
var principals = search.FindAll();
int total = principals.Count();
int numInvalidUsers = RunChecks(principals, new Check[]{
Check1
, Check2
, Check3
});
Score = numInvalidUsers == 0 ? 1 : 0;
}
}
What I'm thinking is that I need to add another parameter to the searchTemplate.SamAccountName
, I'm just not sure how.
Update: I was talking to someone on Reddit who was giving me some helpful suggestions but this user has gone dark. It seems like the most common suggestion is to somehow implement LDAP filters. So if anyone knows how to implement those while still returning principal objects that would be very helpful.