Search Users in Active Directory based on First Na

2019-04-15 05:02发布

I trying to search my organization Active directory for users.

If the FirstName or LastName or DisplayName matches a particular string value, it should return the users.

My Code:

// create your domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.GivenName = "Ramesh*";
// qbeUser.Surname = "Ramesh*";
// qbeUser.DisplayName= "Ramesh*";    

PrincipalSearcher srch = new PrincipalSearcher(qbeUser);

// find all matches
foreach(var found in srch.FindAll())
{
    //
}

The problem is that I am able to search by only one filter.

I am able to AND the filters but not OR. Whether any solutions are available?

2条回答
闹够了就滚
2楼-- · 2019-04-15 05:41

See a possible solution for this issue in this other SO question.

You will need to use the extensibility of UserPrincipal to create a descendant class, in order to get access to the anr property (anr = ambiguous name resolution) which allows searches in multiple name-related properties at once.

查看更多
Luminary・发光体
3楼-- · 2019-04-15 05:53

Have a look at the DirectorySearcher. This article may help.

查看更多
登录 后发表回答