public ActionResult Index(string displayName, string nextLinkUri)
{
QueryOperationResponse<User> response;
var users = DirectoryService.users;
// If a filter query for displayName is submitted, we throw away previous results we were paging.
if (displayName != null)
{
ViewBag.CurrentFilter = displayName;
// Linq query for filter for DisplayName property.
users = (DataServiceQuery<User>)(users.Where(user => user.displayName.StartsWith(displayName)));
response = users.Execute() as QueryOperationResponse<User>;
}
else
{
// Handle the case for first request vs paged request.
if (nextLinkUri == null)
{
response = users.Execute() as QueryOperationResponse<User>;
}
else
{
response = DirectoryService.Execute<User>(new Uri(nextLinkUri)) as QueryOperationResponse<User>;
}
}
List<User> userList = response.ToList();
// Handle the SkipToken if present in the response.
if (response.GetContinuation() != null)
{
ViewBag.ContinuationToken = response.GetContinuation().NextLinkUri;
}
return View(userList);
}
public ActionResult Index(string displayName, string nextLinkUri) { QueryOperationResponse<User> response; var users = DirectoryService.users; // If a filter query for displayName is submitted, we throw away previous results we were paging. if (displayName != null) { ViewBag.CurrentFilter = displayName; // Linq query for filter for DisplayName property. users = (DataServiceQuery<User>)(users.Where(user => user.displayName.StartsWith(displayName))); response = users.Execute() as QueryOperationResponse<User>; } else { // Handle the case for first request vs paged request. if (nextLinkUri == null) { response = users.Execute() as QueryOperationResponse<User>; } else { response = DirectoryService.Execute<User>(new Uri(nextLinkUri)) as QueryOperationResponse<User>; } } List<User> userList = response.ToList(); // Handle the SkipToken if present in the response. if (response.GetContinuation() != null) { ViewBag.ContinuationToken = response.GetContinuation().NextLinkUri; } return View(userList); }
http://github.com/AzureADSamples/WebApp-GraphAPI-DotNet
This C# sample application demonstrates how to Read and Write tenant data to Windows Azure Active Directory using the AAD Graph API, which is a new RESTful interface that allows programmatic access to tenant data such as users, contacts, groups, roles etc. The Graph API is now GA. Graph service enables developers to incorporate new capabilities with existing applications and to build new applications that leverage directory data access to Windows Azure AD.
The walk through for building a .Net application on the AAD Graph team blog provides a deep dive into the steps involved in building any .Net application for accessing Graph service. The blog post covers most of the Graph Service related code present in this sample. If you are actively using AAD Graph Service or have tried it, we would love to hear feedback from you. The team blog is a great place to ask any Graph service related questions and provide us feedback.
The Graph API is compatible with OData V3 and enables applications to construct more complex queries. The sample application authenticates to Graph API by presenting a token that is issued by Windows Azure Access Control Service using OAuth 2.0.
The application is configured and authorized to Read data from a demo company hosted in Windows Azure AD. If you wish to access your own Windows Azure AD tenant or wish to try the Write operations (Create, Delete, Update), then you must use your own tenant. You can manage your existing Azure AD tenant or create a new tenant using the Windows Azure Management portal. You can find more information on the topic for managing your directory from Azure Management Portal here. Once you have the tenant, you need to configure the application for for accessing Graph API. The following topic shows how to enable authentication for accessing Graph API for your application. If you followed the steps in the topic, you will have the Client ID and Key values. Copy these values along with your Tenant Domain Name into the Web.Config of the sample application. Client ID maps to AppPrincipalId, Key value to Password in Web.Config. After you have set up these values in web.config, the application should run against your tenant.
Building the Sample
This sample application can be built using Visual Studio 2012. Visual Studio 2012 Professional or Visual Studio 2012 Ultimate: You can download a free trial here: Visual Studio Free Trial
Install WCF Data Services Client version 5.6.0
Install Active Directory Authentication Library (ADAL) version 1.0.2
You will also need to install ASP.NET MVC 4, which is available from http://www.microsoft.com/en-us/download/details.aspx?id=30683
Description
This C# sample application is an ASP.NET MVC 4 app, which will access the Windows Azure Active Directory Graph API, and query a demo tenant's user information. The full list of capabilities for the Graph API can be found on MSDN (link below).
What you can do with the sample application?