Since you are building application using Office 365 APIs Preview, Azure AD Graph API could be utilized for getting user details (Users and Groups
service on the picture below).
The following example demonstrates how to retrieve users using Azure AD Graph API (excerpt from ASP.NET MVC App):
public static class UserManager
{
const string ServiceResourceId = "https://graph.windows.net/";
static readonly Uri ServiceEndpointUri = new Uri("https://graph.windows.net/");
static DiscoveryContext _discoveryContext;
public static async Task<IEnumerable<IUser>> GetUsers()
{
var client = await EnsureClientCreated();
var userResults = await client.DirectoryObjects.OfType<IUser>().ExecuteAsync();
var allUsers = new List<IUser>();
do
{
allUsers.AddRange(userResults.CurrentPage);
userResults = await userResults.GetNextPageAsync();
} while (userResults != null);
return allUsers;
}
public static async Task<AadGraphClient> EnsureClientCreated()
{
if (_discoveryContext == null)
{
_discoveryContext = await DiscoveryContext.CreateAsync();
}
var dcr = await _discoveryContext.DiscoverResourceAsync(ServiceResourceId);
return new AadGraphClient(new Uri(ServiceEndpointUri, dcr.TenantId), async () =>
{
return (await _discoveryContext.AuthenticationContext.AcquireTokenByRefreshTokenAsync(new SessionCache().Read("RefreshToken"), new Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential(_discoveryContext.AppIdentity.ClientId, _discoveryContext.AppIdentity.ClientSecret), ServiceResourceId)).AccessToken;
});
}
}
Key points:
- The Azure Active Directory Graph API (
AadGraphClient Class
) is used for
getting a user’s detailed properties
- ClientCredential Class is used for authentication in O365 by
providing
client id
and secret
.
Usage
public class O365UserManagementController : Controller
{
public async Task<ActionResult> Index()
{
try
{
var allUsers = await UserManager.GetUsers();
return View(allUsers);
}
catch (RedirectRequiredException ex)
{
return Redirect(ex.RedirectUri.ToString());
}
}
}