|
| 1 | +## Additional Prerequisites |
| 2 | + |
| 3 | + - Azure CLI. [Installation guide](/cli/azure/install-azure-cli) |
| 4 | + |
| 5 | +## Setting Up |
| 6 | + |
| 7 | +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: |
| 8 | + |
| 9 | +- [Azure portal](../../../../../active-directory/managed-identities-azure-resources/qs-configure-portal-windows-vm.md) |
| 10 | +- [Azure PowerShell](../../../../../active-directory/managed-identities-azure-resources/qs-configure-powershell-windows-vm.md) |
| 11 | +- [Azure CLI](../../../../../active-directory/managed-identities-azure-resources/qs-configure-cli-windows-vm.md) |
| 12 | +- [Azure Resource Manager template](../../../../../active-directory/managed-identities-azure-resources/qs-configure-template-windows-vm.md) |
| 13 | +- [Azure Resource Manager SDKs](../../../../../active-directory/managed-identities-azure-resources/qs-configure-sdk-windows-vm.md) |
| 14 | +- [App services](../../../../../app-service/overview-managed-identity.md) |
| 15 | + |
| 16 | +## Authenticate a registered application in the development environment |
| 17 | + |
| 18 | +If your development environment doesn't support single sign-on or login via a web browser, then you can use a registered application to authenticate from the development environment. |
| 19 | + |
| 20 | +### Creating an Azure Active Directory Registered Application |
| 21 | + |
| 22 | +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're 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. |
| 23 | + |
| 24 | +The following example uses the Azure CLI to create a new registered application: |
| 25 | + |
| 26 | +```azurecli |
| 27 | +az ad sp create-for-rbac --name <application-name> --role Contributor --scopes /subscriptions/<subscription-id> |
| 28 | +``` |
| 29 | + |
| 30 | +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. |
| 31 | + |
| 32 | +```json |
| 33 | +{ |
| 34 | + "appId": "generated-app-ID", |
| 35 | + "displayName": "service-principal-name", |
| 36 | + "name": "http://service-principal-uri", |
| 37 | + "password": "generated-password", |
| 38 | + "tenant": "tenant-ID" |
| 39 | +} |
| 40 | +``` |
| 41 | +> [!IMPORTANT] |
| 42 | +> Azure role assignments may take a few minutes to propagate. |
| 43 | +
|
| 44 | +#### Set environment variables |
| 45 | + |
| 46 | +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. |
| 47 | + |
| 48 | +| Environment variable | Value | |
| 49 | +| --------------------- | ---------------------------------------- | |
| 50 | +| `AZURE_CLIENT_ID` | `appId` value from the generated JSON | |
| 51 | +| `AZURE_TENANT_ID` | `tenant` value from the generated JSON | |
| 52 | +| `AZURE_CLIENT_SECRET` | `password` value from the generated JSON | |
| 53 | + |
| 54 | +> [!IMPORTANT] |
| 55 | +> After you set the environment variables, close and re-open your console window. If you're using Visual Studio or another development environment, you may need to restart it in order for it to register the new environment variables. |
| 56 | +
|
| 57 | +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. |
0 commit comments