About this sample
The application uses the Active Directory Authentication Library (ADAL) to get a token from Azure AD using the OAuth 2.0 client credential flow, where the client credential is a certificate.
There's a newer version of this sample! Check it out: https://github.com/azure-samples/ms-identity-dotnetcore-daemon-consoleThis newer sample takes advantage of the Microsoft identity platform(formerly Azure AD v2.0).While still in public preview, every component is supported in production environments
Overview
In this sample, a Windows console application (TodoListDaemonWithCert) calls a web API (TodoListService) using its app identity. This scenario is useful for situations where headless or unattended job or a windows service needs to run with an application identity, instead of a user's identity.
This sample is similar to Daemon-DotNet, except instead of the daemon using a password as a credential to authenticate with Azure AD, it uses a certificate.
Topology
Scenario
Once the service started, when you start the
TodoListDaemon
desktop application, it repeatedly:- adds items to the todo list maintained by the service,
- lists the existing todo list items.
No user interaction is involved in this sample.
How to run this sample
To run this sample, you'll need:
- Visual Studio 2017
- An Internet connection
- An Azure Active Directory (Azure AD) tenant. For more information on how to get an Azure AD tenant, see How to get an Azure AD tenant
- A user account that is an admin of your Azure AD tenant. This sample will not work with a Microsoft account (formerly Windows Live account). Therefore, 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.
Step 1: Clone or download this repository
You can clone this repository from Visual Studio. Alternatively, from your shell or command line, use:
git clone https://github.com/Azure-Samples/active-directory-dotnet-daemon-certificate-credential.git
Given that the name of the sample is pretty long, and so are the name of the referenced NuGet packages, you might want to clone it in a folder close to the root of your hard drive, to avoid file size limitations on Windows.
Step 2: Register the sample with your Azure Active Directory tenant
There are two projects in this sample. Each needs to be separately registered in your Azure AD tenant. To register these projects, you can:
- either follow the steps Step 2: Register the sample with your Azure Active Directory tenant and Step 3: Configure the sample to use your Azure AD tenant
- or use PowerShell scripts that:
- automatically creates the Azure AD applications and related objects (passwords, permissions, dependencies) for you
- modify the Visual Studio projects' configuration files.
If you want to use this automation: 1. On Windows run PowerShell and navigate to the root of the cloned directory 1. In PowerShell run:
PowerShell Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process -Force
1. Run the script to create your Azure AD application and configure the code of the sample application accordingly. PowerShell .\AppCreationScripts\Configure.ps1
> Other ways of running the scripts are described in App Creation ScriptsFor Windows Server 2012, creating a certificate with PowerShell is slightly different: See issue #37
First step: choose the Azure AD tenant where you want to create your applications
As a first step you'll need to:
- Sign in to the Azure portal using either a work or school account or a personal Microsoft account.
- If your account is present in more than one Azure AD tenant, select your profile at the top right corner in the menu on top of the page, and then switch directory. Change your portal session to the desired Azure AD tenant.
Register the service app (TodoListService-Cert)
- Navigate to the Microsoft identity platform for developers App registrations page.
- Select New registration.
- When the Register an application page appears, enter your application's registration information:
- In the Name section, enter a meaningful application name that will be displayed to users of the app, for example
TodoListService-Cert
. - Leave Supported account types on the default setting of Accounts in this organizational directory only.
- In the Name section, enter a meaningful application name that will be displayed to users of the app, for example
- Select Register to create the application.
- On the app Overview page, find the Application (client) ID value and record it for later. You'll need it to configure the Visual Studio configuration file for this project.
- Select the Expose an API section, and:
- Select Add a scope
- accept the proposed Application ID URI (api://{clientId}) by selecting Save and Continue
- Enter the following parameters
- for Scope name use
access_as_application
- Keep Admins and users for Who can consent
- in Admin consent display name type
Access TodoListService-Cert as an application
- in Admin consent description type
Accesses the TodoListService-Cert Web API an application
- in User consent display name type
Access TodoListService-Cert as an application
- in User consent description type
Accesses the TodoListService-Cert Web API as an application
- Keep State as Enabled
- Select Add scope
- for Scope name use
Secure your Web API by defining Application Roles (permission)
If you don't do anything more, Azure AD will provide a token for any daemon application (using the client credential flow) requesting an access token for your Web API (for its App ID URI)
In this step we are going to ensure that Azure AD only provides a token to the applications to which the Tenant admin grants consent. We are going to limit the access to our TodoList client by defining authorizations
Add an app role to the manifest
- While still in the blade for your application, click Manifest.
- Edit the manifest by locating the
appRoles
setting and adding an application roles. The role definition is provided in the JSON block below. Leave theallowedMemberTypes
to "Application" only. - Save the manifest.
The content of
appRoles
should be the following (the id
can be any unique GUID)"appRoles": [
{
"allowedMemberTypes": [ "Application" ],
"description": "Accesses the TodoListService-Cert as an application.",
"displayName": "access_as_application",
"id": "ccf784a6-fd0c-45f2-9c08-2f9d162a0628",
"isEnabled": true,
"lang": null,
"origin": "Application",
"value": "access_as_application"
}
],
Ensure that tokens Azure AD issues tokens for your Web API only to allowed clients
The Web API tests for the app role (that's the developer way of doing it). But you can even ask Azure Active Directory to issue a token for your Web API only to applications which were approved by the tenant admin. For this:
- On the app Overview page for your app registration, select the hyperlink with the name of your application in Managed application in local directory (note this field title can be truncated for instance Managed application in ...)
When you select this link you will navigate to the Enterprise Application Overview page associated with the service principal for your application in the tenant where you created it. You can navigate back to the app registration page by using the back button of your browser.
- Select the Properties page in the Manage section of the Enterprise application pages
- If you want AAD to enforce access to your Web API from only certain clients, set User assignment required? to Yes.
Important security tipBy setting User assignment required? to Yes, AAD will check the app role assignments of the clients when they request an access token for the Web API (see app permissions below). If the client was not be assigned to any AppRoles, AAD would just returninvalid_client: AADSTS501051: Application xxxx is not assigned to a role for the xxxx
If you keep User assignment required? to No, Azure AD won’t check the app role assignments when a client requests an access token to your Web API. Therefore, any daemon client (that is any client using client credentials flow) would still be able to obtain the access token for the Web API just by specifying its audience. Any application, would be able to access the API without having to request permissions for it. Now this is not then end of it, as your Web API can always, as is done in this sample, verify that the application has the right role (which was authorized by the tenant admin), by validating that the access token has a roles claim, and
- Select Save
Register the client app (TodoListDaemon-Cert)
- Navigate to the Microsoft identity platform for developers App registrations page.
- Select New registration.
- When the Register an application page appears, enter your application's registration information:
- In the Name section, enter a meaningful application name that will be displayed to users of the app, for example
TodoListDaemon-Cert
. - Leave Supported account types on the default setting of Accounts in this organizational directory only.
- In the Redirect URI (optional) section, select Web in the combo-box. > Even if this is a desktop application, this is a confidential client application hence the Application Typebeing 'Web', which might seem counter intuitive.
- For the Redirect URI*, enter
https://<your_tenant_name>/TodoListDaemon-Cert
, replacing<your_tenant_name>
with the name of your Azure AD tenant.
- In the Name section, enter a meaningful application name that will be displayed to users of the app, for example
- Select Register to create the application.
- On the app Overview page, find the Application (client) ID value and record it for later. You'll need it to configure the Visual Studio configuration file for this project.
Create a self-signed certificate
To complete this step, you will use the
New-SelfSignedCertificate
Powershell command. You can find more information about the New-SelfSignedCertificate command here.- Open PowerShell and run New-SelfSignedCertificate with the following parameters to create a self-signed certificate in the user certificate store on your computer:
$cert=New-SelfSignedCertificate -Subject "CN=TodoListDaemonWithCert" -CertStoreLocation "Cert:\CurrentUser\My" -KeyExportPolicy Exportable -KeySpec Signature
- If needed, you can later export this certificate using the "Manage User Certificate" MMC snap-in accessible from the Windows Control Panel. You can also add other options to generate the certificate in a different store such as the Computer or service store (See How to: View Certificates with the MMC Snap-in).
Add the certificate for the TodoListDaemon-Cert application in Azure AD
Generate a textual file containing the certificate credentials in a form consumable by AzureAD
Copy and paste the following lines in the same PowerShell window. They generate a text file in the current folder containing information that you can use to upload your certificate to Azure AD:
$bin = $cert.RawData
$base64Value = [System.Convert]::ToBase64String($bin)
$bin = $cert.GetCertHash()
$base64Thumbprint = [System.Convert]::ToBase64String($bin)
$keyid = [System.Guid]::NewGuid().ToString()
$jsonObj = @{customKeyIdentifier=$base64Thumbprint;keyId=$keyid;type="AsymmetricX509Cert";usage="Verify";value=$base64Value}
$keyCredentials=ConvertTo-Json @($jsonObj) | Out-File "keyCredentials.txt"
The content of the generated "keyCredentials.txt" file has the following schema:
[
{
"customKeyIdentifier": "$base64Thumbprint_from_above",
"keyId": "$keyid_from_above",
"type": "AsymmetricX509Cert",
"usage": "Verify",
"value": "$base64Value_from_above"
}
]
Associate the certificate credentials with the Azure AD Application
To associate the certificate credential with the
TodoListDaemon-Cert
app object in Azure AD, you'll need to edit the application manifest. In the Azure portal app registration page for the TodoListDaemon-Cert
, click on Manifest. An editor window opens enabling you to edit the manifest. You need to replace the value of the keyCredentials
property (that is []
if you don't have any certificate credentials yet), with the content of the keyCredential.txt file.
To do this replacement in the manifest, you have two options:
- Option 1: Edit the manifest in place by clicking Edit, replacing the
keyCredentials
value, and then clicking Save. > Note that if you refresh the web page, the key is displayed with different properties than what you have input. In particular, you can now see the endDate, and stateDate, and the value is shown as null. This is normal. - Option 2: Download the manifest to your computer, edit it with your favorite text editor, save a copy of it, and Upload this copy. You might want to choose this option if you want to keep track of the history of the manifest.
Note that the
keyCredentials
property is multi-valued, so you may upload multiple certificates for richer key management. In that case copy only the text between the curly brackets.- Select the API permissions section
- Click the Add a permission button and then,
- Ensure that the My APIs tab is selected
- In the list of APIs, select the API
TodoListService-Cert
. - In the Delegated permissions section, ensure that the right permissions are checked: Access 'TodoListService-Cert'. Use the search box if necessary.
- Select the Add permissions button
- At this stage permissions are assigned correctly but the client app does not allow interaction. Therefore no consent can be presented via a UI and accepted to use the service app. Click the Grant/revoke admin consent for {tenant} button, and then select Yes when you are asked if you want to grant consent for the requested permissions for all account in the tenant. You need to be an Azure AD tenant admin to do this.
Step 3: Configure the sample to use your Azure AD tenant
In the steps below, "ClientID" is the same as "Application ID" or "AppId".
Open the solution in Visual Studio to configure the projects
Configure the service project
- Open the
TodoListService\Web.Config
file - Find the app key
ida:Tenant
and replace the existing value with your Azure AD tenant name. - Find the app key
ida:Audience
and replace the existing value with the App ID URI you registered earlier in the form ofapi://{clientId}
.
Configure the client project
- Open the
TodoListDaemonWithCert\App.Config
file - Find the app key
ida:Tenant
and replace the existing value with your Azure AD tenant name. - Find the app key
ida:ClientId
and replace the existing value with the application ID (clientId) of theTodoListDaemon
application copied from the Azure portal. - Find the app key
ida:CertName
and replace the existing value with Certificate. - Find the app key
todo:TodoListResourceId
and replace the existing value with the App ID URI you registered earlier in the form ofapi://{clientId}
. - Find the app key
todo:TodoListBaseAddress
and replace the existing value with the base address of the TodoListService project (by defaulthttps://localhost:44321/
).
Step 4: Run the sample
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. To do this, you can for instance:
- Right click on the solution in the solution explorer and choose Set Startup projects from the context menu.
- choose Multiple startup projects
- TodoListDaemonWithCert: Start
- TodoListService: Start Start without debugging
- In the Visual Studio tool bar, press the start button: a web window appears running the service and a console application runs the daemon application under debugger. you can set breakpoints to understand the call to ADAL.NET.
The daemon will add items to the To Do list and then read them back.
How to deploy this sample to Azure
This project has one WebApp / Web API projects. To deploy them to Azure Web Sites, you'll need, for each one, to:
- create an Azure Web Site
- publish the Web App / Web APIs to the web site, and
- update its client(s) to call the web site instead of IIS Express.
Create and publish the TodoListService-Cert
to an Azure Web Site
- Sign in to the Azure portal.
- Click
Create a resource
in the top left-hand corner, select Web --> Web App, and give your web site a name, for example,TodoListService-Cert-contoso.azurewebsites.net
. - Thereafter select the
Subscription
,Resource Group
,App service plan and Location
.OS
will be Windows andPublish
will be Code. - Click
Create
and wait for the App Service to be created. - Once you get the
Deployment succeeded
notification, then click onGo to resource
to navigate to the newly created App service. - Once the web site is created, locate it it in the Dashboard and click it to open App Services Overview screen.
- From the Overview tab of the App Service, download the publish profile by clicking the Get publish profile link and save it. Other deployment mechanisms, such as from source control, can also be used.
- Switch to Visual Studio and go to the TodoListService-Cert project. Right click on the project in the Solution Explorer and select Publish. Click Import Profile on the bottom bar, and import the publish profile that you downloaded earlier.
- Click on Configure and in the
Connection tab
, update the Destination URL so that it is ahttps
in the home page url, for example https://TodoListService-Cert-contoso.azurewebsites.net. Click Next. - On the Settings tab, make sure
Enable Organizational Authentication
is NOT selected. Click Save. Click on Publish on the main screen. - Visual Studio will publish the project and automatically open a browser to the URL of the project. If you see the default web page of the project, the publication was successful.
Update the Active Directory tenant application registration for TodoListService-Cert
- Navigate back to to the Azure portal. In the left-hand navigation pane, select the Azure Active Directory service, and then select App registrations (Preview).
- In the resultant screen, select the
TodoListService-Cert
application. - From the Branding menu, update the Home page URL, to the address of your service, for example https://TodoListService-Cert-contoso.azurewebsites.net. Save the configuration.
- Add the same URL in the list of values of the Authentication -> Redirect URIs menu. If you have multiple redirect urls, make sure that there a new entry using the App service's Uri for each redirect url.
Update the TodoListDaemon-Cert
to call the TodoListService-Cert
Running in Azure Web Sites
- In Visual Studio, go to the
TodoListDaemon-Cert
project. - Open
TodoListDaemonWithCert\App.Config
. Only one change is needed - update thetodo:TodoListBaseAddress
key value to be the address of the website you published, for example, https://TodoListService-Cert-contoso.azurewebsites.net. - Run the client! If you are trying multiple different client types (for example, .Net, Windows Store, Android, iOS) you can have them all call this one published web API.
NOTE: Remember, the To Do list is stored in memory in this TodoListService sample. Azure Web Sites will spin down your web site if it is inactive, and your To Do list will get emptied. Also, if you increase the instance count of the web site, requests will be distributed among the instances. ToDo list will, therefore, not be the same on each instance.
About the Code
Client side: the daemon app
The code acquiring a token is entirely located in the
TodoListDaemonWithCert\Program.cs
file. The AuthenticationContext
is created (line 76)authContext = new AuthenticationContext(authority);
Then a
ClientAssertionCertificate
is instantiated line 87, from the TodoListDaemon
application's Client ID and a certificate (cert
) which was found from the certificate store (see lines 72-89).certCred = new ClientAssertionCertificate(clientId, cert);
This instance of
ClientAssertionCertificate
is used in the GetAccessToken()
method is as an argument to AcquireTokenAsync
to get a token for the Web API (line 147) GetAccessToken()
is itself called from PostTodo()
and GetTodo()
methods.result = await authContext.AcquireTokenAsync(todoListResourceId, certCred);
This token is then used as a bearer token to call the Web API (line 186 and 216)
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken)
Service side: how we protected the API
On the service side, the code directing ASP.NET to validate the access token is in
App_Start\Startup.Auth.cs
. It only validates the audience of the application (the App ID URI) public partial class Startup
{
// For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
public void ConfigureAuth(IAppBuilder app)
{
app.UseWindowsAzureActiveDirectoryBearerAuthentication(
new WindowsAzureActiveDirectoryBearerAuthenticationOptions
{
Tenant = ConfigurationManager.AppSettings["ida:Tenant"],
TokenValidationParameters = new TokenValidationParameters
{
ValidAudience = ConfigurationManager.AppSettings["ida:Audience"]
}
});
}
}
However, the controllers also validate that the client has a
roles
claim of value access_as_application
. It returns an Unauthorized error otherwise. public IEnumerable<TodoItem> Get()
{
//
// The roles claim tells what permissions the client application has in the service.
// In this case we look for a roles value of access_as_application
//
Claim scopeClaim = ClaimsPrincipal.Current.FindFirst("roles");
if (scopeClaim == null || (scopeClaim.Value != "access_as_application"))
{
throw new HttpResponseException(new HttpResponseMessage { StatusCode = HttpStatusCode.Unauthorized,
ReasonPhrase = "The 'roles' claim does not contain 'access_as_application'or was not found" });
}
...
}
How to recreate this sample
First, in Visual Studio 2017 (or above) create an empty solution to host the projects. Then, follow these steps to create each project.
Creating the TodoListService 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.
- In the
Models
folder, add a new class calledTodoItem.cs
. Copy the implementation of TodoItem 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
TodoListController
resolving missing references by addingusing
statements forSystem.Collections.Concurrent
,TodoListService.Models
,System.Security.Claims
.
Creating the TodoListDaemon Project
- In the solution, create a new Windows --> Console Application called TodoListDaemon.
- 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 the same name into this class, completely replacing the code in the new file. - Copy the code from
Program.cs
in the sample project into the file of the same name in the new project, completely replacing the code in the new file. - In
app.config
create keys forida:AADInstance
,ida:Tenant
,ida:ClientId
,ida:CertName
,todo:TodoListResourceId
, andtodo:TodoListBaseAddress
and set them accordingly. For the global Azure cloud, the value ofida:AADInstance
ishttps://login.microsoftonline.com/{0}
.
Finally, in the properties of the solution itself, set both projects as startup projects.
Community Help and Support
Use Stack Overflow to get support from the community. Ask your questions on Stack Overflow first and browse existing issues to see if someone has asked your question before. Make sure that your questions or comments are tagged with [
adal
dotnet
].
If you find a bug in the sample, please raise the issue on GitHub Issues.
To provide a recommendation, visit the following User Voice page.
No comments:
Post a Comment