Question

I am attempting to search active directory based on a full name of 'LAST,FIRST MIDDLEINITIAL' The problem is, these are three seperate fields in AD, but I have only a single string to search with. Is there a way, using active directory to, to search on combined fields similar to the way it can be done in SQL? ex in SQL:

SELECT * FROM tbltest WHERE (Last + ',' + First + ' ' + MiddleI) = 'LAST,FIRST MIDDLEI'

Something like below in active directory perhaps?:

"(sn+','+givenname+' '+initials = LAST,FIRST MIDDLEI)"
Was it helpful?

Solution

If you're using the AccountManagement (http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement%28v=vs.110%29.aspx) namespace you can do something like:

using (var context = new PrincipalContext(ContextType.Domain, FQDC))
{
    UserPrincipal up = new UserPrincipal(context);

    up.GivenName = First;
    up.MiddleName = MiddleI;
    up.Surname = Last;

    PrincipalSearcher ps = new PrincipalSearcher();
    ps.QueryFilter = up;

    PrincipalSearchResult<Principal> results = ps.FindOne();

    if (results != null) ...
}

If you're using SDS with say a DirectorySearcher you would set the filter property:

searcher.Filter = string.Format("(&(objectclass=user)(lastname={0})(firstname={1})(middlename={2}))", Last, First, MiddleI);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top