0

I was wondering if there was a way to find a User in Active Directory that is LIKE what a user has typed and then post it back to a previous page within the application.

I have the following code

using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{               
    UserPrincipal queryAdUser = new UserPrincipal(ctx);
    queryAdUser.SamAccountName = "Thomas";
    using (PrincipalSearcher searchAdUser = new PrincipalSearcher(qbeUser))
    {
        foreach (UserPrincipal UsersFound in searchAdUser.FindAll())
        {
            DirectoryEntry res = UsersFound.GetUnderlyingObject() as DirectoryEntry;
            Response.Write(res.Properties["givenName"].Value);
        }
    }
}

This currently only returns the user that matches the entered value by the user. So lets say I entered a search of "T" and I had in Active Directory "Tom, Ted and Tim" I want to return these users as results when the user searches.

Is it possable to alter the above code to perform this task, or this there a better way that I could be doing this?

Kind Regards

Masoud Mohammadi
  • 1,721
  • 1
  • 23
  • 41
tparky
  • 33
  • 1
  • 9
  • You can add wildcards (asterisks) around the account name: `queryAdUser.SamAccountName = "*T*"`. That would return all accounts with a T in it. Or you can just have a trailing wildcard to find all accounts that start with a T. Whatever, you know how wildcards work. – itsme86 Nov 03 '14 at 21:13
  • See the duplicate mentioned - it has an answer - basically, you need to use `queryAdUser.SamAccountName = "T*";` – marc_s Nov 03 '14 at 21:26

0 Answers0