Azure Active Directory Code Samples
You can use Microsoft Azure Active Directory (Azure AD) to add authentication and authorization to your web applications and web APIs. This section links you to code samples that show you how it's done and code snippets that you can use in your applications. On the code sample page, you'll find detailed read-me topics that help with requirements, installation and set-up. And the code is commented to help you understand the critical sections.
To understand the basic scenario for each sample type, see Authentication Scenarios for Azure AD.
Contribute to our samples on GitHub: Microsoft Azure Active Directory Samples and Documentation (https://github.com/AzureADSamples)
In this topic:
Web Browser to Web Application
Single Page Application (SPA)
Native Application to Web API
Web Application to Web API
Server or Daemon Application to Web API
Calling Azure AD Graph API
Authorization
Legacy Walkthroughs
Web Browser to Web Application
These samples show how to write a web application directs the user’s browser to sign them in to Azure AD.
Language/Platform | Sample | Description |
---|---|---|
C#/.NET |
Use OpenID Connect (ASP.Net OpenID Connect OWIN middleware) to authenticate users from an Azure AD tenant. |
|
C#/.NET |
A multi-tenant .NET MVC web application that uses OpenID Connect (ASP.Net OpenID Connect OWIN middleware) to authenticate users from multiple Azure AD tenants. |
|
C#/.NET |
Use WS-Federation (ASP.Net WS-Federation OWIN middleware) to authenticate users from an Azure AD tenant. |
Single Page Application (SPA)
This sample shows how to write a single page application secured with Azure AD.
Language/Platform | Sample | Description |
---|---|---|
JavaScript, C#/.NET |
Use ADAL for JavaScript and Azure AD to secure an AngularJS-based single page app implemented with an ASP.NET web API back end. |
Native Application to Web API
These code samples show how to build native client applications that call web APIs that are secured by Azure AD. They use Azure AD Authentication Library (ADAL) and OAuth 2.0 in Azure AD.
Language/Platform | Sample | Description |
---|---|---|
C#/.NET |
A .NET WPF application that calls a web API that is secured by using Azure AD. |
|
C#/.NET |
A Windows Store application that calls a web API that is secured with Azure AD. |
|
C#/.NET |
A Windows Store application calling a multi-tenant web API that is secured with Azure AD. |
|
C#/.NET |
A native client application that calls a web API, which gets a token to act on behalf of the original user, and then uses the token to call another web API. |
|
C#/.NET |
A Windows Store application for Windows Phone 8.1 that calls a web API that is secured by Azure AD. |
|
ObjC |
An iOS application that calls a web API that requires Azure AD for authentication. |
|
C#/.NET |
A native client application that includes logic to process a JWT token in a web API, instead of using OWIN middleware. |
|
C#/Xamarin |
A Xamarin binding to the native Azure AD Authentication Library (ADAL) for the Android library.e |
|
C#/Xamarin |
A Xamarin binding to the native Azure AD Authentication Library (ADAL) for iOS. |
|
C#/Xamarin |
A Xamarin project that targets five platforms and calls a web API that is secured by Azure AD. |
|
C#/.NET |
A native application that performs non-interactive authentication and calls a web API that is secured by Azure AD. |
Web Application to Web API
These code samples show how use OAuth 2.0 in Azure AD to build web applications that call web APIs that are secured by Azure AD.
Language/Platform | Sample | Description |
---|---|---|
C#/.NET |
Call a web API with the signed-in user's permissions. |
|
C#/.NET |
Call a web API with the application's permissions. |
|
C#/.NET |
Add authorization with OAuth 2.0 in Azure AD to an existing web application so it can call a web API. |
|
JavaScript |
Set up a REST API service that's integrated with Azure AD for API protection. Includes a Node.js server with a Web API. |
|
C#/.NET |
A multi-tenant MVC web application that uses OpenID Connect (ASP.Net OpenID Connect OWIN middleware) to authenticate users from an Azure AD tenant. Uses an authorization code to invoke the Graph API. |
Server or Daemon Application to Web API
These code samples show how to build a daemon or server application that gets resources from a web API by using Azure AD Authentication Library (ADAL)and OAuth 2.0 in Azure AD.
Language/Platform | Sample | Description |
---|---|---|
C#/.NET |
A console application calls a web API. The client credential is a password. |
|
C#/.NET |
A console application that calls a web API. The client credential is a certificate. |
Calling Azure AD Graph API
These code sample show how to build applications that call the Azure AD Graph API to read and write directory data.
Language/Platform | Sample | Description |
---|---|---|
Java |
A web application that uses the Graph API to access Azure AD directory data. |
|
PHP |
A web application that uses the Graph API to access Azure AD directory data. |
|
C#/.NET |
A web application that uses the Graph API to access Azure AD directory data. |
|
C#/.NET |
This console app demonstrates common Read and Write calls to the Graph API, and shows how to execute user license assignment and update a user's thumbnail photo and links. |
|
C#/.NET |
A console application that uses the differential query in the Graph API to get periodic changes to user objects in an Azure AD tenant. |
|
C#/.NET |
An MVC application uses Graph API queries to generate a simple company organizational chart. |
|
PHP |
A PHP application that calls the Graph API to register an extension and then read, update, and delete values in the extension attribute. |
Authorization
These code samples show how to use Azure AD for authorization.
Language/Platform | Sample | Description |
---|---|---|
C#/.NET |
Perform role based access control (RBAC) using Azure Active Directory group claims in an application that is integrated with Azure AD. |
|
C#/.NET |
Perform role based access control (RBAC) using Azure Active Directory application roles in an application that is integrated with Azure AD. |
Legacy Walkthroughs
These walkthroughs use slightly older technology, but still might be of interest.
Language/Platform | Name | Description |
---|---|---|
C#/.NET |
Role-Based and ACL-Based Authorization in a Windows Azure AD Application |
Perform role-based authorization (RBAC) and ACL-based authorization in an application that is integrated with Azure AD. |
C#/.NET |
Use Azure AD Authentication Library (ADAL) (formerly AAL) for Windows Store Beta to add user authentication capabilities to a Windows Store app. |
|
C#/.NET |
ADAL - Native App to REST service - Authentication with AAD via Browser Dialog |
Use Azure AD Authentication Library (ADAL) to add user authentication capabilities to a WPF client. |
C#/.NET |
ADAL - Native App to REST service - Authentication with ACS via Browser Dialog |
Use Azure AD Authentication Library (ADAL) and Access Control Service 2.0 (ACS) to add user authentication capabilities to a WPF client. |
C#/.NET |
Use Azure AD Authentication Library (ADAL) to secure service calls from a server side process to an MVC4 Web API REST service. |
|
C#/.NET |
Configure a .NET application to perform web single sign-on against your Azure AD enterprise directory. |
|
C#/.NET |
Use Azure AD to add to the single sign-on and directory access capabilities of one .NET application to work across multiple organizations. |
|
JAVA |
Use the Graph API to access directory data from Azure AD. |
|
PHP |
Use the Graph API to access directory data from Azure AD. |
|
C#/.NET |
Use the Graph API to access directory data from Azure AD. |
|
C#/.NET |
Use the differential query in the Graph API to get periodic changes to user objects in an Azure AD tenant. |
|
C#/.NET |
Sample App for Integrating Multi-Tenant Cloud Application for Azure AD |
Integrate a multi-tenant application into Azure AD. |
C#/.NET |
Securing a Windows Store Application and REST Web Service Using Azure AD (Preview) |
Create a simple web API resource and a Windows Store client application using Azure AD and the Azure AD Authentication Library (ADAL). |
C#/.NET |
Configure a Microsoft .NET application to use the Azure AD Graph API to access data from an Azure AD tenant directory. |
See Also
Other Resources
Azure AD Graph API Helper Library
Developing Modern Applications using OAuth and Active Directory Federation Services