In this sample, native client calls a web API and then the web API calls another downstream web API after obtaining a token to act On Behalf Of the original user. The sample uses the Active Directory Authentication Library (ADAL) in the native client to obtain a token for the user to call the first web API, and also in the first web API to get a token to act on behalf of the user to call the second web API. Both flows use the OAuth 2.0 protocol to obtain the tokens.
For more information about how the protocols work in this scenario and other scenarios, see Authentication Scenarios for Azure AD.
To run this sample you will need:
- Visual Studio 2013
- An Internet connection
- An Azure subscription (a free trial is sufficient)
Every Azure subscription has an associated Azure Active Directory tenant. If you don't already have an Azure subscription, you can get a free subscription by signing up at http://wwww.windowsazure.com. All of the Azure AD features used by this sample are available free of charge.
From your shell or command line:
git clone https://github.com/AzureADSamples/WebAPI-OnBehalfOf-DotNet.git
If you already have a user account in your Azure Active Directory tenant, you can skip to the next step. This sample will not work with a Microsoft account, so if you signed in to the Azure portal with a Microsoft account and have never created a user account in your directory before, you need to do that now. If you create an account and want to use it to sign-in to the Azure portal, don't forget to add the user account as a co-administrator of your Azure subscription.
There are two projects in this sample. Each needs to be separately registered in your Azure AD tenant.
- Sign in to the Azure management portal.
- Click on Active Directory in the left hand nav.
- Click the directory tenant where you wish to register the sample application.
- Click the Applications tab.
- In the drawer, click Add.
- Click "Add an application my organization is developing".
- Enter a friendly name for the application, for example "TodoListService", select "Web Application and/or Web API", and click next.
- For the sign-on URL, enter the base URL for the sample, which is by default
https://localhost:44321. - For the App ID URI, enter
https://<your_tenant_name>/TodoListService, replacing<your_tenant_name>with the name of your Azure AD tenant. Click OK to complete the registration. - While still in the Azure portal, click the Configure tab of your application.
- Find the Client ID value and copy it aside, you will need this later when configuring your application.
- Create a new key for the application. Save the configuration so you can view the key value. Save this aside for when you configure the project in Visual Studio.
- Using the Manage Manifest button in the drawer, download the manifest file for the application.
- Add a permission to the application by replacing the appPermissions section with the block of JSON below. You will need to create a new GUID and replace the example permissionId GUID.
- Using the Manage Manfiest button, upload the updated manifest file. Save the configuration of the app.
"oauth2Permissions": [
{
"adminConsentDescription": "Allow full access to the To Do List service on behalf of the signed-in user",
"adminConsentDisplayName": "Have full access to the To Do List service",
"id": "b69ee3c9-c40d-4f2a-ac80-961cd1534e40",
"isEnabled": true,
"origin": "Application",
"type": "User",
"userConsentDescription": "Allow full access to the To Do service on your behalf",
"userConsentDisplayName": "Have full access to the To Do service",
"value": "user_impersonation"
}
],NOTE: In this sample, the TodoListService makes a delegated identity call to the Graph API to read the user's profile. By default, when the TodoListService is registered with Active Directory, it is configured to request this permission in the "Permissions to Other Applications" configuration section. If you modify the TodoListService to call a different API, or if you build your own service that makes an On Behalf Of call, the service it calls and the permissions it requires must be added to the "Permissions to Other Applications" configuration in Azure AD.s
- Sign in to the Azure management portal.
- Click on Active Directory in the left hand nav.
- Click the directory tenant where you wish to register the sample application.
- Click the Applications tab.
- In the drawer, click Add.
- Click "Add an application my organization is developing".
- Enter a friendly name for the application, for example "TodoListClient-DotNet", select "Native Client Application", and click next.
- For the Redirect URI, enter
http://TodoListClient. Click finish. - Click the Configure tab of the application.
- Find the Client ID value and copy it aside, you will need this later when configuring your application.
- In "Permissions to Other Applications", select the TodoListService, and request the delegated permission "Have full access to the To Do List service". Save the configuration.
- Open the solution in Visual Studio 2013.
- Open the
web.configfile. - Find the app key
ida:Tenantand replace the value with your AAD tenant name. - Find the app key
ida:Audienceand replace the value with the App ID URI you registered earlier, for examplehttps://<your_tenant_name>/TodoListService. - Find the app key
ida:ClientIdand replace the value with the Client ID for the TodoListService from the Azure portal. - Find the app key
ida:AppKeyand replace the value with the key for the TodoListService from the Azure portal.
- Open `app.config'.
- Find the app key
ida:Tenantand replace the value with your AAD tenant name. - Find the app key
ida:ClientIdand replace the value with the Client ID for the TodoListClient from the Azure portal. - Find the app key
ida:RedirectUriand replace the value with the Redirect URI for the TodoListClient from the Azure portal, for examplehttp://TodoListClient. - Find the app key
todo:TodoListResourceIdand replace the value with the App ID URI of the TodoListService, for examplehttps://<your_tenant_name>/TodoListService - Find the app key
todo:TodoListBaseAddressand replace the value with the base address of the TodoListService project.
Since the web API is SSL protected, the client of the API (the web app) will refuse the SSL connection to the web API unless it trusts the API's SSL certificate. Use the following steps in Windows Powershell to trust the IIS Express SSL certificate. You only need to do this once. If you fail to do this step, calls to the TodoListService will always throw an unhandled exception where the inner exception message is:
"The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel."
To configure your computer to trust the IIS Express SSL certificate, begin by opening a Windows Powershell command window as Administrator.
Query your personal certificate store to find the thumbprint of the certificate for CN=localhost:
PS C:\windows\system32> dir Cert:\LocalMachine\My
Directory: Microsoft.PowerShell.Security\Certificate::LocalMachine\My
Thumbprint Subject
---------- -------
C24798908DA71693C1053F42A462327543B38042 CN=localhost
Next, add the certificate to the Trusted Root store:
PS C:\windows\system32> $cert = (get-item cert:\LocalMachine\My\C24798908DA71693C1053F42A462327543B38042)
PS C:\windows\system32> $store = (get-item cert:\Localmachine\Root)
PS C:\windows\system32> $store.Open("ReadWrite")
PS C:\windows\system32> $store.Add($cert)
PS C:\windows\system32> $store.Close()
You can verify the certificate is in the Trusted Root store by running this command:
PS C:\windows\system32> dir Cert:\LocalMachine\Root
Clean the solution, rebuild the solution, and run it. You might want to go into the solution properties and set both projects as startup projects, with the service project starting first.
Explore the sample by signing in, adding items to the To Do list, removing the user account, and starting again. The To Do list service will take the user's access token, received from the client, and use it to get another access token so it can act On Behalf Of the user in the Graph API. This sample does not cache the user's access token at the To Do list service, so it requests a new access token on every request. The service could cache the access token in a database, for example, for better performance, and it could cache the refresh token so that it could obtain access tokens for the user even when the user is not present.
Coming soon.
Coming soon.
First, in Visual Studio 2013 create an empty solution to host the projects. Then, follow these steps to create each project.
- In the solution, create a new ASP.Net MVC web API project called TodoListService and while creating the project, click the Change Authentication button, select Organizational Accounts, Cloud - Single Organization, enter the name of your Azure AD tenant, and set the Access Level to Single Sign On. You will be prompted to sign-in to your Azure AD tenant. NOTE: You must sign-in with a user that is in the tenant; you cannot, during this step, sign-in with a Microsoft account.
- Add the pre-release Active Directory Authentication Library (ADAL) NuGet, Microsoft.IdentityModel.Clients.ActiveDirectory, version 2.6.1-alpha (or higher), to the project.
- In the
Modelsfolder add a new class calledTodoItem.cs. Copy the implementation of TodoItem from this sample into the class. - In the
Modelsfolder add a new class calledUserProfile.cs. Copy the implementation of UserProfile from this sample into the class. - Add a new, empty, Web API 2 controller called
TodoListController. - Copy the implementation of the TodoListController from this sample into the controller. Don't forget to add the
[Authorize]attribute to the class. - In
TodoListControllerresolving missing references by addingusingstatements forSystem.Collections.Concurrent,TodoListService.Models,System.Security.Claims. - In
web.configcreate keys forida:AADInstance,ida:Tenant,ida:ClientId, andida:AppKey,and set them accordingly. For the public Azure cloud, the value ofida:AADInstanceishttps://login.windows.net/{0}. - In
web.config, in<appSettings>, create keys forida:GraphResourceIdandida:GraphUserUrland set the values accordingly. For the public Azure AD, the value ofida:GraphResourceIdishttps://graph.windows.net, and the value ofida:GraphUserUrlishttps://graph.windows.net/{0}/me?api-version=2013-11-08.
- In the solution, create a new Windows --> WPF Application called TodoListClient.
- Add the (stable) Active Directory Authentication Library (ADAL) NuGet, Microsoft.IdentityModel.Clients.ActiveDirectory, version 1.0.3 (or higher) to the project.
- Add assembly references to
System.Net.Http,System.Web.Extensions, andSystem.Configuration. - Add a new class to the project called
TodoItem.cs. Copy the code from the sample project file of same name into this class, completely replacing the code in the file in the new project. - Add a new class to the project called
CacheHelper.cs. Copy the code from the sample project file of same name into this class, completely replacing the code in the file in the new project. - Add a new class to the project called
CredManCache.cs. Copy the code from the sample project file of same name into this class, completely replacing the code in the file in the new project. - Copy the markup from `MainWindow.xaml' in the sample project into the file of same name in the new project, completely replacing the markup in the file in the new project.
- Copy the code from
MainWindow.xaml.csin the sample project into the file of same name in the new project, completely replacing the code in the file in the new project. - In
app.configcreate keys forida:AADInstance,ida:Tenant,ida:ClientId,ida:RedirectUri,todo:TodoListResourceId, andtodo:TodoListBaseAddressand set them accordingly. For the public Azure cloud, the value ofida:AADInstanceishttps://login.windows.net/{0}.
Finally, in the properties of the solution itself, set both projects as startup projects.