|
| 1 | +# How to use certificates instead of secrets in your client applications |
| 2 | + |
| 3 | +We recommend you familiarize yourself with [Using certificates with Microsoft\.Identity\.Web](https://github.com/AzureAD/microsoft-identity-web/wiki/Certificates#getting-certificates-from-key-vault) as it provides various ways for a developer to use a certificate instead of a client secret to authenticate their apps with Azure AD. |
| 4 | +> Note: Please carefully go through [Getting certificates from Key Vault](https://github.com/AzureAD/microsoft-identity-web/wiki/Certificates#getting-certificates-from-key-vault) when deploying your app to production. |
| 5 | +
|
| 6 | +## Using a Client certificate with KeyVault |
| 7 | + |
| 8 | +This sample was configured to use a client secret, but have an option to use a certificate instead. |
| 9 | + |
| 10 | +### To be able to use a certificate, please make the following changes: |
| 11 | + |
| 12 | +1. Open Client/appsettings.json file |
| 13 | +1. **Comment out** the next line: |
| 14 | + |
| 15 | +```json |
| 16 | +"ClientSecret": "[Copy the client secret added to the app from the Azure portal]" |
| 17 | +``` |
| 18 | + |
| 19 | +1. **Un-comment** the following lines: |
| 20 | + |
| 21 | +```json |
| 22 | +"ClientCertificates": [ |
| 23 | + { |
| 24 | + "SourceType": "KeyVault", |
| 25 | + "KeyVaultUrl": "[Enter URL for you KeyVault]", |
| 26 | + "KeyVaultCertificateName": "TodoListClient-aspnetcore-webapi" |
| 27 | + } |
| 28 | +] |
| 29 | +``` |
| 30 | + |
| 31 | +1. While inside the sample folder, open a Powershell terminal |
| 32 | + |
| 33 | +1. Set next execution policy |
| 34 | + |
| 35 | +```powershell |
| 36 | +Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process -Force |
| 37 | +``` |
| 38 | + |
| 39 | +1. Run the Cleanup.ps1 script to delete any existing old App Registration for the sample |
| 40 | + |
| 41 | +```powershell |
| 42 | +AppCreationScripts-withCert/Cleanup.ps1 |
| 43 | +``` |
| 44 | + |
| 45 | +1. Run the AppCreationScripts-withCert/Configure.ps1 script to re-create the App Registration. The script will also create a [application name].pfx file that will be **manually** uploaded into Key Vault. When asked about a password, remember it - you will need the password when uploading the certificate. |
| 46 | + |
| 47 | +```powershell |
| 48 | +AppCreationScripts-withCert/Configure.ps1 |
| 49 | +``` |
| 50 | + |
| 51 | +1. To use KeyVault, sign in to the [Azure portal](https://portal.azure.com) and [create an Azure Key Vault](https://docs.microsoft.com/azure/key-vault/general/quick-create-portal) |
| 52 | +1. Inside Client/appsettings.json file - update "KeyVaultUrl" key to have URL of your Key Vault, like https://[your Key Vault name here].vault.azure.net |
| 53 | +1. [Upload](https://docs.microsoft.com/azure/key-vault/certificates/tutorial-import-certificate#import-a-certificate-to-key-vault) the generated AppCreationScripts-withCert\.PFX file into the Key Vault |
| 54 | +1. Run the sample as indicated in [README.md](README.md) |
| 55 | +1. Use the account you used to upload the certificate to key vault to sign-into the web app. |
| 56 | +1. In production environments, you'd give access to your deployed web app or Virtual machine to read this certificate's Key Vault entry. |
| 57 | + |
| 58 | +## Using a local Client certificate |
| 59 | + |
| 60 | +1. Open Client/appsettings.json file |
| 61 | +2. **Comment out** the next line: |
| 62 | + |
| 63 | +```json |
| 64 | +"ClientSecret": "[Copy the client secret added to the app from the Azure portal]" |
| 65 | +``` |
| 66 | + |
| 67 | +1. **Un-comment** the following lines: |
| 68 | + |
| 69 | +```json |
| 70 | +"ClientCertificates": [ |
| 71 | + { |
| 72 | + "SourceType": "StoreWithDistinguishedName", |
| 73 | + "CertificateStorePath": "CurrentUser/My", |
| 74 | + "CertificateDistinguishedName": "CN=TodoListClient-aspnetcore-webapi" |
| 75 | + } |
| 76 | +] |
| 77 | +``` |
| 78 | + |
| 79 | +1. While inside the sample folder, open a Powershell terminal |
| 80 | + |
| 81 | +1. Set next execution policy |
| 82 | + |
| 83 | +```powershell |
| 84 | +Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process -Force |
| 85 | +``` |
| 86 | + |
| 87 | +1. Run the *Cleanup.ps1* script to delete any existing old App Registration for the sample |
| 88 | + |
| 89 | +```powershell |
| 90 | +AppCreationScripts-withCert/Cleanup.ps1 -TenantId "[Optional] - your tenant id" -AzureEnvironmentName "[Optional] - Azure environment, defaults to 'Global'" |
| 91 | +``` |
| 92 | + |
| 93 | +1. Run the AppCreationScripts-withCert/Configure.ps1 script to re-create the App Registration. The script will also create a [application name].pfx file that will be **manually** uploaded into Key Vault. When asked about a password, remember it - you will need the password when uploading the certificate. |
| 94 | + |
| 95 | +```powershell |
| 96 | +AppCreationScripts-withCert/Configure.ps1 -TenantId "[Optional] - your tenant id" -AzureEnvironmentName "[Optional] - Azure environment, defaults to 'Global'" |
| 97 | +``` |
| 98 | + |
| 99 | +1. Run the sample as indicated in [README.md](README.md) |
| 100 | + |
| 101 | +## More information |
| 102 | + |
| 103 | +### Using Azure KeyVault |
| 104 | + |
| 105 | +Cloud applications and services use cryptographic keys and secrets to help keep information secure. [Azure KeyVault](https://azure.microsoft.com/services/key-vault/) safeguards these keys and secrets. When you use Key Vault, you can encrypt authentication keys, storage account keys, data encryption keys, .pfx files, and passwords by using keys that are protected by hardware security modules (HSMs). |
| 106 | + |
| 107 | +### About Managed Identities for Azure Resources |
| 108 | + |
| 109 | +[Azure KeyVault](https://azure.microsoft.com/services/key-vault/#product-overview) |
| 110 | + |
| 111 | +[Managed Identities for Azure Resources](https://docs.microsoft.com/azure/active-directory/managed-identities-azure-resources/) |
| 112 | + |
| 113 | +[Managed Identities for Azure App Services](https://docs.microsoft.com/azure/app-service/overview-managed-identity?tabs=dotnet) |
0 commit comments