Skip to content

Commit d54456c

Browse files
authored
Merge pull request #189109 from barclayn/mi-clarity-04
minor edits
2 parents 6d2d2d1 + 765031c commit d54456c

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

articles/active-directory/managed-identities-azure-resources/how-to-use-vm-token.md

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ ms.subservice: msi
1212
ms.topic: how-to
1313
ms.tgt_pltfrm: na
1414
ms.workload: identity
15-
ms.date: 01/11/2022
15+
ms.date: 02/18/2022
1616
ms.author: barclayn
1717
ms.collection: M365-identity-device-management
1818
---
@@ -23,7 +23,7 @@ ms.collection: M365-identity-device-management
2323

2424
Managed identities for Azure resources provide Azure services with an automatically managed identity in Azure Active Directory. You can use this identity to authenticate to any service that supports Azure AD authentication, without having credentials in your code.
2525

26-
This article provides various code and script examples for token acquisition, as well as guidance on important topics such as handling token expiration and HTTP errors.
26+
This article provides various code and script examples for token acquisition. It also contains guidance about handling token expiration and HTTP errors.
2727

2828
## Prerequisites
2929

@@ -40,7 +40,7 @@ If you plan to use the Azure PowerShell examples in this article, be sure to ins
4040
4141
## Overview
4242

43-
A client application can request managed identities for Azure resources [app-only access token](../develop/developer-glossary.md#access-token) for accessing a given resource. The token is [based on the managed identities for Azure resources service principal](overview.md#managed-identity-types). As such, there is no need for the client to register itself to obtain an access token under its own service principal. The token is suitable for use as a bearer token in
43+
A client application can request a managed identity [app-only access token](../develop/developer-glossary.md#access-token) to access a given resource. The token is [based on the managed identities for Azure resources service principal](overview.md#managed-identity-types). As such, there's no need for the client to obtain an access token under its own service principal. The token is suitable for use as a bearer token in
4444
[service-to-service calls requiring client credentials](../develop/v2-oauth2-client-creds-grant-flow.md).
4545

4646
| Link | Description |
@@ -58,7 +58,7 @@ A client application can request managed identities for Azure resources [app-onl
5858

5959
## Get a token using HTTP
6060

61-
The fundamental interface for acquiring an access token is based on REST, making it accessible to any client application running on the VM that can make HTTP REST calls. This is similar to the Azure AD programming model, except the client uses an endpoint on the virtual machine (vs an Azure AD endpoint).
61+
The fundamental interface for acquiring an access token is based on REST, making it accessible to any client application running on the VM that can make HTTP REST calls. This approach is similar to the Azure AD programming model, except the client uses an endpoint on the virtual machine (vs an Azure AD endpoint).
6262

6363
Sample request using the Azure Instance Metadata Service (IMDS) endpoint *(recommended)*:
6464

@@ -72,7 +72,7 @@ GET 'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-0
7272
| `http://169.254.169.254/metadata/identity/oauth2/token` | The managed identities for Azure resources endpoint for the Instance Metadata Service. |
7373
| `api-version` | A query string parameter, indicating the API version for the IMDS endpoint. Use API version `2018-02-01` or greater. |
7474
| `resource` | A query string parameter, indicating the App ID URI of the target resource. It also appears in the `aud` (audience) claim of the issued token. This example requests a token to access Azure Resource Manager, which has an App ID URI of `https://management.azure.com/`. |
75-
| `Metadata` | An HTTP request header field, required by managed identities for Azure resources as a mitigation against Server Side Request Forgery (SSRF) attack. This value must be set to "true", in all lower case. |
75+
| `Metadata` | An HTTP request header field required by managed identities. This information is used as a mitigation against server side request forgery (SSRF) attacks. This value must be set to "true", in all lower case. |
7676
| `object_id` | (Optional) A query string parameter, indicating the object_id of the managed identity you would like the token for. Required, if your VM has multiple user-assigned managed identities.|
7777
| `client_id` | (Optional) A query string parameter, indicating the client_id of the managed identity you would like the token for. Required, if your VM has multiple user-assigned managed identities.|
7878
| `mi_res_id` | (Optional) A query string parameter, indicating the mi_res_id (Azure Resource ID) of the managed identity you would like the token for. Required, if your VM has multiple user-assigned managed identities. |
@@ -95,20 +95,20 @@ Content-Type: application/json
9595

9696
| Element | Description |
9797
| ------- | ----------- |
98-
| `access_token` | The requested access token. When calling a secured REST API, the token is embedded in the `Authorization` request header field as a "bearer" token, allowing the API to authenticate the caller. |
98+
| `access_token` | The requested access token. When you call a secured REST API, the token is embedded in the `Authorization` request header field as a "bearer" token, allowing the API to authenticate the caller. |
9999
| `refresh_token` | Not used by managed identities for Azure resources. |
100100
| `expires_in` | The number of seconds the access token continues to be valid, before expiring, from time of issuance. Time of issuance can be found in the token's `iat` claim. |
101101
| `expires_on` | The timespan when the access token expires. The date is represented as the number of seconds from "1970-01-01T0:0:0Z UTC" (corresponds to the token's `exp` claim). |
102102
| `not_before` | The timespan when the access token takes effect, and can be accepted. The date is represented as the number of seconds from "1970-01-01T0:0:0Z UTC" (corresponds to the token's `nbf` claim). |
103103
| `resource` | The resource the access token was requested for, which matches the `resource` query string parameter of the request. |
104104
| `token_type` | The type of token, which is a "Bearer" access token, which means the resource can give access to the bearer of this token. |
105105

106-
## Get a token using the Azure Identity client library
106+
## Get a token using the Azure identity client library
107107

108-
This is the reccomended method and library for using Azure Managed identities. All Azure SDKs are integrated with the Azure.Identity library that provides support for DefaultAzureCredential. This class makes it easy to use Managed Identities with Azure SDKs.[Learn more](https://docs.microsoft.com/dotnet/api/overview/azure/identity-readme)
108+
Using the Azure identity client library is the recommended way to use managed identities. All Azure SDKs are integrated with the ```Azure.Identity``` library that provides support for DefaultAzureCredential. This class makes it easy to use Managed Identities with Azure SDKs.[Learn more](/dotnet/api/overview/azure/identity-readme)
109109

110110
1. Install the [Azure.Identity](https://www.nuget.org/packages/Azure.Identity) package and other required [Azure SDK library packages](https://aka.ms/azsdk), such as [Azure.Security.KeyVault.Secrets](https://www.nuget.org/packages/Azure.Security.KeyVault.Secrets/).
111-
2. Use the sample code below. Note that you need not worry about getting tokens. You can directly use the Azure SDK clients. The code is for demonstrating how to get the token, if you need to.
111+
2. Use the sample code below. You don't need to worry about getting tokens. You can directly use the Azure SDK clients. The code is for demonstrating how to get the token, if you need to.
112112

113113
```csharp
114114
using Azure.Core;
@@ -126,7 +126,7 @@ This is the reccomended method and library for using Azure Managed identities. A
126126

127127
## Get a token using the Microsoft.Azure.Services.AppAuthentication library for .NET
128128

129-
For .NET applications and functions, the simplest way to work with managed identities for Azure resources is through the Microsoft.Azure.Services.AppAuthentication package. This library will also allow you to test your code locally on your development machine, using your user account from Visual Studio, the [Azure CLI](/cli/azure), or Active Directory Integrated Authentication. For more on local development options with this library, see the [Microsoft.Azure.Services.AppAuthentication reference](/dotnet/api/overview/azure/service-to-service-authentication). This section shows you how to get started with the library in your code.
129+
For .NET applications and functions, the simplest way to work with managed identities for Azure resources is through the Microsoft.Azure.Services.AppAuthentication package. This library will also allow you to test your code locally on your development machine. You can test your code using your user account from Visual Studio, the [Azure CLI](/cli/azure), or Active Directory Integrated Authentication. For more on local development options with this library, see the [Microsoft.Azure.Services.AppAuthentication reference](/dotnet/api/overview/azure/service-to-service-authentication). This section shows you how to get started with the library in your code.
130130

131131
1. Add references to the [Microsoft.Azure.Services.AppAuthentication](https://www.nuget.org/packages/Microsoft.Azure.Services.AppAuthentication) and [Microsoft.Azure.KeyVault](https://www.nuget.org/packages/Microsoft.Azure.KeyVault) NuGet packages to your application.
132132
@@ -310,7 +310,7 @@ The following example demonstrates how to use the managed identities for Azure r
310310
Invoke-WebRequest -Uri 'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fmanagement.azure.com%2F' -Headers @{Metadata="true"}
311311
```
312312

313-
Example on how to parse the access token from the response:
313+
Example of how to parse the access token from the response:
314314
```azurepowershell
315315
# Get an access token for managed identities for Azure resources
316316
$response = Invoke-WebRequest -Uri 'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fmanagement.azure.com%2F' `
@@ -333,7 +333,7 @@ curl 'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-
333333
```
334334

335335

336-
Example on how to parse the access token from the response:
336+
Example of how to parse the access token from the response:
337337

338338
```bash
339339
response=$(curl 'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fmanagement.azure.com%2F' -H Metadata:true -s)
@@ -343,7 +343,8 @@ echo The managed identities for Azure resources access token is $access_token
343343

344344
## Token caching
345345

346-
While the managed identities for Azure resources subsystem does cache tokens, we also recommend to implement token caching in your code. As a result, you should prepare for scenarios where the resource indicates that the token is expired.
346+
The managed identities subsystem caches tokens but we still recommend that you implement token caching in your code.
347+
You should prepare for scenarios where the resource indicates that the token is expired.
347348

348349
On-the-wire calls to Azure AD result only when:
349350

@@ -352,14 +353,14 @@ On-the-wire calls to Azure AD result only when:
352353

353354
## Error handling
354355

355-
The managed identities for Azure resources endpoint signals errors via the status code field of the HTTP response message header, as either 4xx or 5xx errors:
356+
The managed identities endpoint signals errors via the status code field of the HTTP response message header, as either 4xx or 5xx errors:
356357

357358
| Status Code | Error Reason | How To Handle |
358359
| ----------- | ------------ | ------------- |
359360
| 404 Not found. | IMDS endpoint is updating. | Retry with Exponential Backoff. See guidance below. |
360361
| 429 Too many requests. | IMDS Throttle limit reached. | Retry with Exponential Backoff. See guidance below. |
361-
| 4xx Error in request. | One or more of the request parameters was incorrect. | Do not retry. Examine the error details for more information. 4xx errors are design-time errors.|
362-
| 5xx Transient error from service. | The managed identities for Azure resources subsystem or Azure Active Directory returned a transient error. | It is safe to retry after waiting for at least 1 second. If you retry too quickly or too often, IMDS and/or Azure AD may return a rate limit error (429).|
362+
| 4xx Error in request. | One or more of the request parameters was incorrect. | Don't retry. Examine the error details for more information. 4xx errors are design-time errors.|
363+
| 5xx Transient error from service. | The managed identities for Azure resources subsystem or Azure Active Directory returned a transient error. | It's safe to retry after waiting for at least 1 second. If you retry too quickly or too often, IMDS and/or Azure AD may return a rate limit error (429).|
363364
| timeout | IMDS endpoint is updating. | Retry with Exponential Backoff. See guidance below. |
364365

365366
If an error occurs, the corresponding HTTP response body contains JSON with the error details:
@@ -375,22 +376,22 @@ This section documents the possible error responses. A "200 OK" status is a succ
375376

376377
| Status code | Error | Error Description | Solution |
377378
| ----------- | ----- | ----------------- | -------- |
378-
| 400 Bad Request | invalid_resource | AADSTS50001: The application named *\<URI\>* was not found in the tenant named *\<TENANT-ID\>*. This can happen if the application has not been installed by the administrator of the tenant or consented to by any user in the tenant. You might have sent your authentication request to the wrong tenant.\ | (Linux only) |
379+
| 400 Bad Request | invalid_resource | AADSTS50001: The application named *\<URI\>* wasn't found in the tenant named *\<TENANT-ID\>*. This message shows if the tenant administrator hasn't installed the application or no tenant user consented to it. You might have sent your authentication request to the wrong tenant.\ | (Linux only) |
379380
| 400 Bad Request | bad_request_102 | Required metadata header not specified | Either the `Metadata` request header field is missing from your request, or is formatted incorrectly. The value must be specified as `true`, in all lower case. See the "Sample request" in the preceding REST section for an example.|
380381
| 401 Unauthorized | unknown_source | Unknown Source *\<URI\>* | Verify that your HTTP GET request URI is formatted correctly. The `scheme:host/resource-path` portion must be specified as `http://localhost:50342/oauth2/token`. See the "Sample request" in the preceding REST section for an example.|
381382
| | invalid_request | The request is missing a required parameter, includes an invalid parameter value, includes a parameter more than once, or is otherwise malformed. | |
382-
| | unauthorized_client | The client is not authorized to request an access token using this method. | Caused by a request on a VM that doesn't have managed identities for Azure resources configured correctly. See [Configure managed identities for Azure resources on a VM using the Azure portal](qs-configure-portal-windows-vm.md) if you need assistance with VM configuration. |
383+
| | unauthorized_client | The client isn't authorized to request an access token using this method. | Caused by a request on a VM that doesn't have managed identities for Azure resources configured correctly. See [Configure managed identities for Azure resources on a VM using the Azure portal](qs-configure-portal-windows-vm.md) if you need assistance with VM configuration. |
383384
| | access_denied | The resource owner or authorization server denied the request. | |
384-
| | unsupported_response_type | The authorization server does not support obtaining an access token using this method. | |
385+
| | unsupported_response_type | The authorization server doesn't support obtaining an access token using this method. | |
385386
| | invalid_scope | The requested scope is invalid, unknown, or malformed. | |
386-
| 500 Internal server error | unknown | Failed to retrieve token from the Active directory. For details see logs in *\<file path\>* | Verify that managed identities for Azure resources is enabled on the VM. See [Configure managed identities for Azure resources on a VM using the Azure portal](qs-configure-portal-windows-vm.md) if you need assistance with VM configuration.<br><br>Also verify that your HTTP GET request URI is formatted correctly, particularly the resource URI specified in the query string. See the "Sample request" in the preceding REST section for an example, or [Azure services that support Azure AD authentication](./services-support-managed-identities.md) for a list of services and their respective resource IDs.
387+
| 500 Internal server error | unknown | Failed to retrieve token from the Active directory. For details see logs in *\<file path\>* | Verify that the VM has managed identities for Azure resources enabled. See [Configure managed identities for Azure resources on a VM using the Azure portal](qs-configure-portal-windows-vm.md) if you need assistance with VM configuration.<br><br>Also verify that your HTTP GET request URI is formatted correctly, particularly the resource URI specified in the query string. See the "Sample request" in the preceding REST section for an example, or [Azure services that support Azure AD authentication](./services-support-managed-identities.md) for a list of services and their respective resource IDs.
387388

388389
> [!IMPORTANT]
389390
> - IMDS is not intended to be used behind a proxy and doing so is unsupported. For examples of how to bypass proxies, refer to the [Azure Instance Metadata Samples](https://github.com/microsoft/azureimds).
390391
391392
## Retry guidance
392393

393-
It is recommended to retry if you receive a 404, 429, or 5xx error code (see [Error handling](#error-handling) above).
394+
It's recommended to retry if you receive a 404, 429, or 5xx error code (see [Error handling](#error-handling) above).
394395

395396
Throttling limits apply to the number of calls made to the IMDS endpoint. When the throttling threshold is exceeded, IMDS endpoint limits any further requests while the throttle is in effect. During this period, the IMDS endpoint will return the HTTP status code 429 ("Too many requests"), and the requests fail.
396397

@@ -402,7 +403,7 @@ For retry, we recommend the following strategy:
402403

403404
## Resource IDs for Azure services
404405

405-
See [Azure services that support Azure AD authentication](./services-support-managed-identities.md) for a list of resources that support Azure AD and have been tested with managed identities for Azure resources, and their respective resource IDs.
406+
See [Azure Services with managed identities support](managed-identities-status.md) for a list of resources that support managed identities for Azure resources.
406407

407408

408409
## Next steps

0 commit comments

Comments
 (0)