Authenticating to Azure AD in daemon apps with certificates

Utolsó frissítés: 2017.01.25.
Szerkesztés a GitHubon

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.

For more information about how the protocols work in this scenario and other scenarios, see Authentication Scenarios for Azure AD and Service to service calls using client credentials

Looking for previous versions of this code sample? Check out the tags on the releases GitHub page.

How to Run this sample

To run this sample, you will need: - Visual Studio 2013 or above - An Internet connection - An Azure Active Directory (Azure AD) tenant. For more information on how to get an Azure AD tenant, please see How to get an Azure AD tenant

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

Step 2: Register the sample with your Azure Active Directory tenant

There are two projects in this sample. Each project needs to be separately registered in your Azure AD tenant.

Register the TodoListService web API

  1. Sign in to the Azure portal.
  2. On the top bar, click on your account and under the Directory list, choose the Active Directory tenant where you wish to register your application.
  3. Click on More Services in the left hand nav, and choose Azure Active Directory.
  4. Click on App registrations and choose Add.
  5. Enter a friendly name for the application, for example 'TodoListService' and select 'Web Application and/or Web API' as the Application Type. 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 on Create to create the application.
  6. While still in the Azure portal, choose your application, click on Settings and choose Properties.
  7. Find the Application ID value and copy it to the clipboard.

Register the TodoListDaemonWithCert app

  1. Sign in to the Azure portal.
  2. On the top bar, click on your account and under the Directory list, choose the Active Directory tenant where you wish to register your application.
  3. Click on More Services in the left hand nav, and choose Azure Active Directory.
  4. Click on App registrations and choose Add.
  5. Enter a friendly name for the application, for example 'TodoListDaemonWithCert' and select 'Web Application and/or Web API' as the Application Type (even if here we have a console application).
  6. Since this application is a daemon and not a web application, it doesn't have a sign-in URL or app ID URI. For these two fields, enter "http://TodoListDaemonWithCert". Click on Create to create the application.
  7. While still in the Azure portal, choose your application, click on Settings and choose Properties.
  8. Find the Application ID value and copy it to the clipboard.

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-SelfSignedCertificat 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 as a key for the TodoListDaemonWithCert 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 sam 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 TodoListDaemonWithCert app object in Azure AD, you will need to edit the application manifest. In the Azure Management Portal app registration for the TodoListDaemonWithCert click on Manifest. A blade 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 vlaue 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.

Step 3: Configure the sample to use your Azure AD tenant

Configure the TodoListDaemon project

  1. Open `app.config'.
  2. Find the app key ida:Tenant and replace the value with your AAD tenant name.
  3. Find the app key ida:ClientId and replace the value with the Client ID for the TodoListDaemonWithCert app registration from the Azure portal.
  4. Find the app key ida:CertName and replace the value with the subject name of the self-signed certificate you created, e.g. "CN=TodoListDaemonWithCert".
  5. Find the app key todo:TodoListResourceId and replace the value with the App ID URI of the TodoListService, for example https://<your_tenant_name>/TodoListService
  6. Find the app key todo:TodoListBaseAddress and replace the value with the base address of the TodoListService project.

Configure the TodoListService project

  1. Open the solution in Visual Studio 2013.
  2. Open the web.config file.
  3. Find the app key ida:Tenant and replace the value with your AAD tenant name.
  4. Find the app key ida:Audience and replace the value with the App ID URI you registered earlier, for example https://<your_tenant_name>/TodoListService.
  5. Find the app key ida:ClientId and replace the value with the Client ID for the TodoListService from the Azure portal.

Step 4: Trust the IIS Express SSL certificate

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

Step 5: 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.

The daemon will add items to its To Do list and then read them back.

How to deploy this sample to Azure

Coming soon.

About the Code

If you've looked at the code in this sample and are wondering how authorization works, you're not alone. See this Stack Overflow question. The TodoList Service in this solution simply validates that the client was able to authenticate against the tenant that the service is configured to work with. Effectively, any application in that tenant will be able to use the service.

How to recreate this sample

First, in Visual Studio 2013 (or above) create an empty solution to host the projects. Then, follow these steps to create each project.

Creating the TodoListService Project

  1. 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.
  2. In the Models folder add a new class called TodoItem.cs. Copy the implementation of TodoItem from this sample into the class.
  3. Add a new, empty, Web API 2 controller called TodoListController.
  4. Copy the implementation of the TodoListController from this sample into the controller. Don't forget to add the [Authorize] attribute to the class.
  5. In TodoListController resolving missing references by adding using statements for System.Collections.Concurrent, TodoListService.Models, System.Security.Claims.

Creating the TodoListDaemon Project

  1. In the solution, create a new Windows --> Console Application called TodoListDaemon.
  2. Add the (stable) Active Directory Authentication Library (ADAL) NuGet, Microsoft.IdentityModel.Clients.ActiveDirectory, version 1.0.3 (or higher) to the project.
  3. Add assembly references to System.Net.Http, System.Web.Extensions, and System.Configuration.
  4. 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.
  5. Copy the code from Program.cs in the sample project into the file of same name in the new project, completely replacing the code in the file in the new project.
  6. In app.config create keys for ida:AADInstance, ida:Tenant, ida:ClientId, ida:CertName, todo:TodoListResourceId, and todo:TodoListBaseAddress and set them accordingly. For the public Azure cloud, the value of ida:AADInstance is https://login.windows.net/{0}.

Finally, in the properties of the solution itself, set both projects as startup projects.