1
votes

If I have a Active Directory setup in my server whose domain is say "mydomain.com", I will create LDAP DirectorySearcher for this as followes.

string domainPath = "LDAP://mydomain.com";
DirectoryEntry entry = new DirectoryEntry(domainPath, "userName", "password");
DirectorySearcher searcher = new DirectorySearcher(entry);

What will be the domainPath, If I want to create DirectorySearcher for my Office 365 Active Directory?

P.S : I have synced the Active Directory of the server with Office 365 using 'AzureADSync'

1

1 Answers

1
votes

LDAP is only available for on-premises Active Directory.

For Azure Active Directory, you need to use the Graph APIs (either the Microsoft Graph or the Azure Active Directory Graph. See this link for info on how to pick between one and the other)

You'll need to register your application, set the right permissions depending on what you want to do, and have code similar to this snippet:

Note: This particular snippet returns all users in the directory:

var authority = "https://login.microsoftonline.com/";
var resource = "https://graph.windows.net/";

var tenant = "mydomain.com";
var clientId = <YourClientID>;
var redirectUri = <YourRedirectUri>;

var ctx = new AuthenticationContext(authority + tenant);

var graphUri = resource + tenant;
var client = new ActiveDirectoryClient(new Uri(graphUri), 
    async () => { 
        var token = await ctx.AcquireTokenAsync(resource, clientId, new Uri(redirectUri), new PlatformParameters(PromptBehavior.Always)); 
        return token.AccessToken;
    });


var users = await client.Users.ExecuteAsync();
users.CurrentPage.Select(u => u.DisplayName).Dump();

Here's a link to Azure AD Graph samples. That have instructions on app registration, setting up permissions and also how to query the graph from different platforms/scenarios.