You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
25
25
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.
27
27
28
28
## Prerequisites
29
29
@@ -40,7 +40,7 @@ If you plan to use the Azure PowerShell examples in this article, be sure to ins
40
40
41
41
## Overview
42
42
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
@@ -58,7 +58,7 @@ A client application can request managed identities for Azure resources [app-onl
58
58
59
59
## Get a token using HTTP
60
60
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).
62
62
63
63
Sample request using the Azure Instance Metadata Service (IMDS) endpoint *(recommended)*:
64
64
@@ -72,7 +72,7 @@ GET 'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-0
72
72
|`http://169.254.169.254/metadata/identity/oauth2/token`| The managed identities for Azure resources endpoint for the Instance Metadata Service. |
73
73
|`api-version`| A query string parameter, indicating the API version for the IMDS endpoint. Use API version `2018-02-01` or greater. |
74
74
|`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. |
76
76
|`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.|
77
77
|`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.|
78
78
|`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. |
|`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. |
99
99
|`refresh_token`| Not used by managed identities for Azure resources. |
100
100
|`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. |
101
101
|`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). |
102
102
|`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). |
103
103
|`resource`| The resource the access token was requested for, which matches the `resource` query string parameter of the request. |
104
104
|`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. |
105
105
106
-
## Get a token using the Azure Identity client library
106
+
## Get a token using the Azure identity client library
107
107
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)
109
109
110
110
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.
112
112
113
113
```csharp
114
114
usingAzure.Core;
@@ -126,7 +126,7 @@ This is the reccomended method and library for using Azure Managed identities. A
126
126
127
127
## Get a token using the Microsoft.Azure.Services.AppAuthentication library for .NET
128
128
129
-
For .NETapplicationsandfunctions, thesimplestwaytoworkwithmanagedidentitiesfor 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), orActiveDirectoryIntegratedAuthentication. Formoreonlocaldevelopmentoptionswiththislibrary, seethe [Microsoft.Azure.Services.AppAuthenticationreference](/dotnet/api/overview/azure/service-to-service-authentication). Thissectionshowsyouhowtogetstartedwiththelibraryinyourcode.
129
+
For .NETapplicationsandfunctions, thesimplestwaytoworkwithmanagedidentitiesfor 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), orActiveDirectoryIntegratedAuthentication. Formoreonlocaldevelopmentoptionswiththislibrary, seethe [Microsoft.Azure.Services.AppAuthenticationreference](/dotnet/api/overview/azure/service-to-service-authentication). Thissectionshowsyouhowtogetstartedwiththelibraryinyourcode.
130
130
131
131
1. Addreferencestothe [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.
132
132
@@ -310,7 +310,7 @@ The following example demonstrates how to use the managed identities for Azure r
|4xxErrorinrequest. |Oneormoreoftherequestparameterswasincorrect. |Don't retry. Examine the error details for more information. 4xx errors are design-time errors.|
363
+
|5xxTransienterrorfromservice. |ThemanagedidentitiesforAzureresourcessubsystemorAzureActiveDirectoryreturnedatransienterror. |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).|
|400BadRequest|invalid_resource|AADSTS50001: Theapplicationnamed*\<URI\>*wasn't found in the tenant named *\<TENANT-ID\>*. This message shows if the tenant administrator hasn'tinstalledtheapplicationornotenantuserconsentedtoit. Youmighthavesentyourauthenticationrequesttothewrongtenant.\ | (Linuxonly) |
|401Unauthorized|unknown_source|UnknownSource*\<URI\>*|VerifythatyourHTTPGETrequestURIisformattedcorrectly. The `scheme:host/resource-path` portionmustbespecifiedas `http://localhost:50342/oauth2/token`. See the "Sample request" in the preceding REST section for an example.|
||unauthorized_client|Theclientisnotauthorizedtorequestanaccesstokenusingthismethod. |CausedbyarequestonaVMthatdoesn'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|Theclientisn't authorized to request an access token using this method. | Caused by a request on a VM that doesn'thavemanagedidentitiesforAzureresourcesconfiguredcorrectly. See [ConfiguremanagedidentitiesforAzureresourcesonaVMusingtheAzureportal](qs-configure-portal-windows-vm.md) ifyouneedassistancewithVMconfiguration. |
Itisrecommendedtoretryifyoureceivea 404, 429, or5xxerrorcode (see [Errorhandling](#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).
394
395
395
396
ThrottlinglimitsapplytothenumberofcallsmadetotheIMDSendpoint. Whenthethrottlingthresholdisexceeded, IMDSendpointlimitsanyfurtherrequestswhilethethrottleisineffect. Duringthisperiod, theIMDSendpointwillreturntheHTTPstatuscode429 ("Too many requests"), andtherequestsfail.
396
397
@@ -402,7 +403,7 @@ For retry, we recommend the following strategy:
402
403
403
404
## Resource IDs for Azure services
404
405
405
-
See [AzureservicesthatsupportAzureADauthentication](./services-support-managed-identities.md) foralistofresourcesthatsupportAzureADandhavebeentestedwithmanagedidentitiesforAzureresources, andtheirrespectiveresourceIDs.
406
+
See [AzureServiceswithmanagedidentitiessupport](managed-identities-status.md) foralistofresourcesthatsupportmanagedidentitiesforAzureresources.
0 commit comments