|
| 1 | +--- |
| 2 | +title: Create an Azure Active Directory Service Principal from the Azure CLI |
| 3 | +titleSuffix: An Azure Communication Services quickstart |
| 4 | +description: In this quick start we'll create an application and service principal to authenticate with Azure Communication Services. |
| 5 | +services: azure-communication-services |
| 6 | +author: jbeauregardb |
| 7 | +ms.service: azure-communication-services |
| 8 | +ms.subservice: identity |
| 9 | +ms.topic: quickstart |
| 10 | +ms.date: 06/30/2021 |
| 11 | +ms.author: jbeauregardb |
| 12 | +ms.reviewer: mikben |
| 13 | +ms.custom: mode-api, devx-track-azurecli |
| 14 | +ms.devlang: azurecli |
| 15 | +--- |
| 16 | + |
| 17 | +# Quickstart: Authenticate using Azure Active Directory (Azure CLI) |
| 18 | + |
| 19 | +The Azure Identity SDK provides Azure Active Directory (Azure AD) token authentication support for Azure SDK packages. The latest versions of the Azure Communication Services SDKs for .NET, Java, Python, and JavaScript integrate with the Azure Identity library to provide a simple and secure means to acquire an OAuth 2.0 token for authorization of Azure Communication Services requests. |
| 20 | + |
| 21 | +An advantage of the Azure Identity SDK is that it enables you to use the same code to authenticate across multiple services whether your application is running in the development environment or in Azure. |
| 22 | + |
| 23 | +The Azure Identity SDK can authenticate with many methods. In Development we'll be using a service principal tied to a registered application, with credentials stored in Environnment Variables this is suitable for testing and development. |
| 24 | + |
| 25 | +## Prerequisites |
| 26 | + |
| 27 | + - Azure CLI. [Installation guide](/cli/azure/install-azure-cli) |
| 28 | + - An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free) |
| 29 | + |
| 30 | +## Setting Up |
| 31 | + |
| 32 | +When using Active Directory for other Azure Resources, you should be using Managed identities. To learn how to enable managed identities for Azure Resources, see one of these articles: |
| 33 | + |
| 34 | +- [Azure portal](../../../active-directory/managed-identities-azure-resources/qs-configure-portal-windows-vm.md) |
| 35 | +- [Azure PowerShell](../../../active-directory/managed-identities-azure-resources/qs-configure-powershell-windows-vm.md) |
| 36 | +- [Azure CLI](../../../active-directory/managed-identities-azure-resources/qs-configure-cli-windows-vm.md) |
| 37 | +- [Azure Resource Manager template](../../../active-directory/managed-identities-azure-resources/qs-configure-template-windows-vm.md) |
| 38 | +- [Azure Resource Manager SDKs](../../../active-directory/managed-identities-azure-resources/qs-configure-sdk-windows-vm.md) |
| 39 | +- [App services](../../../app-service/overview-managed-identity.md) |
| 40 | + |
| 41 | +## Authenticate a registered application in the development environment |
| 42 | + |
| 43 | +If your development environment does not support single sign-on or login via a web browser, then you can use a registered application to authenticate from the development environment. |
| 44 | + |
| 45 | +### Creating an Azure Active Directory Registered Application |
| 46 | + |
| 47 | +To create a registered application from the Azure CLI, you need to be logged in to the Azure account where you want the operations to take place. To do this, you can use the `az login` command and enter your credentials in the browser. Once you are logged in to your Azure account from the CLI, we can call the `az ad sp create-for-rbac` command to create the registered application and service principal. |
| 48 | + |
| 49 | +The following examples uses the Azure CLI to create a new registered application |
| 50 | + |
| 51 | +```azurecli |
| 52 | +az ad sp create-for-rbac --name <application-name> --role Contributor --scopes /subscriptions/<subscription-id> |
| 53 | +``` |
| 54 | + |
| 55 | +The `az ad sp create-for-rbac` command will return a list of service principal properties in JSON format. Copy these values so that you can use them to create the necessary environment variables in the next step. |
| 56 | + |
| 57 | +```json |
| 58 | +{ |
| 59 | + "appId": "generated-app-ID", |
| 60 | + "displayName": "service-principal-name", |
| 61 | + "name": "http://service-principal-uri", |
| 62 | + "password": "generated-password", |
| 63 | + "tenant": "tenant-ID" |
| 64 | +} |
| 65 | +``` |
| 66 | +> [!IMPORTANT] |
| 67 | +> Azure role assignments may take a few minutes to propagate. |
| 68 | +
|
| 69 | +#### Set environment variables |
| 70 | + |
| 71 | +The Azure Identity SDK reads values from three environment variables at runtime to authenticate the application. The following table describes the value to set for each environment variable. |
| 72 | + |
| 73 | +| Environment variable | Value | |
| 74 | +| --------------------- | ---------------------------------------- | |
| 75 | +| `AZURE_CLIENT_ID` | `appId` value from the generated JSON | |
| 76 | +| `AZURE_TENANT_ID` | `tenant` value from the generated JSON | |
| 77 | +| `AZURE_CLIENT_SECRET` | `password` value from the generated JSON | |
| 78 | + |
| 79 | +> [!IMPORTANT] |
| 80 | +> After you set the environment variables, close and re-open your console window. If you are using Visual Studio or another development environment, you may need to restart it in order for it to register the new environment variables. |
| 81 | +
|
| 82 | +Once these variables have been set, you should be able to use the DefaultAzureCredential object in your code to authenticate to the service client of your choice. |
| 83 | + |
| 84 | +## Next steps |
| 85 | + |
| 86 | +> [!div class="nextstepaction"] |
| 87 | +> [Learn about authentication](../../concepts/authentication.md) |
| 88 | +
|
| 89 | +You may also want to: |
| 90 | + |
| 91 | +- [Learn more about Azure Identity library](/dotnet/api/overview/azure/identity-readme) |
0 commit comments