Alexander
Alexander

Reputation: 20224

Microsoft Exchange: How to get AD domain via EWS?

I get an Exchange email address, and I have to fetch the AD entry of the user with that email address.

As long as I only have one GC, this works fine using DirectoryEntry:

DirectorySearcher searcher = new DirectorySearcher(new DirectoryEntry("GC:"));
searcher.Filter = string.Format("{0}={1}","mail","[email protected]");
DirectoryEntry entry = searcher.FindOne().GetDirectoryEntry();

But if I have multiple trusted GCs, and the user that is querying AD is in a different GC than the user queried, the object cannot be found in the global catalog, because I did not provide the AD domain to search in.

So either I query all GCs (can I find all trusted GCs in a GC?) or I get the DirectoryEntry using EWS.

Which of these two possibilities would work?

Upvotes: 0

Views: 519

Answers (1)

baldpate
baldpate

Reputation: 1739

I don't quite understand what you mean:

But if I have multiple trusted GCs, and the user that is querying AD is in a different GC than the user queried, the object cannot be found in the global catalog, because I did not provide the AD domain to search in.

A GC only contains objects in its own forest.
You cannot find a user located in forest1 by querying a GC in forest2.

To specify the GC to use

DirectorySearcher searcher = new DirectorySearcher(
    new DirectoryEntry("GC://forest1.net/"));

You may need to provide credential of another user if current user don't have permission to read from that forest:

DirectorySearcher searcher = new DirectorySearcher(
    new DirectoryEntry("GC://forest1.net/", "user", "password"));

Upvotes: 1

Related Questions