title | titleSuffix | description | author | ms.author | ms.service | ms.custom | ms.topic | ms.date | zone_pivot_groups |
---|---|---|---|---|---|---|---|---|---|
Use managed identities to access App Configuration |
Azure App Configuration |
Authenticate to Azure App Configuration using managed identities |
AlexandraKemperMS |
alkemper |
azure-app-configuration |
devx-track-csharp, fasttrack-edit, subject-rbac-steps |
conceptual |
04/08/2021 |
appconfig-provider |
Azure Active Directory managed identities simplify secrets management for your cloud application. With a managed identity, your code can use the service principal created for the Azure service it runs on. You use a managed identity instead of a separate credential stored in Azure Key Vault or a local connection string.
Azure App Configuration and its .NET Core, .NET Framework, and Java Spring client libraries have managed identity support built into them. Although you aren't required to use it, the managed identity eliminates the need for an access token that contains secrets. Your code can access the App Configuration store using only the service endpoint. You can embed this URL in your code directly without exposing any secret.
:::zone target="docs" pivot="framework-dotnet"
This article shows how you can take advantage of the managed identity to access App Configuration. It builds on the web app introduced in the quickstarts. Before you continue, Create an ASP.NET Core app with App Configuration first.
:::zone-end
:::zone target="docs" pivot="framework-spring"
This article shows how you can take advantage of the managed identity to access App Configuration. It builds on the web app introduced in the quickstarts. Before you continue, Create a Java Spring app with Azure App Configuration first.
:::zone-end
Important
Managed Identity cannot be used to authenticate locally-running applications. Your application must be deployed to an Azure service that supports Managed Identity. This article uses Azure App Service as an example, but the same concept applies to any other Azure service that supports managed identity, for example, Azure Kubernetes Service, Azure Virtual Machine, and Azure Container Instances. If your workload is hosted in one of those services, you can leverage the service's managed identity support, too.
You can use any code editor to do the steps in this tutorial. Visual Studio Code is an excellent option available on the Windows, macOS, and Linux platforms.
In this article, you learn how to:
[!div class="checklist"]
- Grant a managed identity access to App Configuration.
- Configure your app to use a managed identity when you connect to App Configuration.
To complete this tutorial, you must have:
:::zone target="docs" pivot="framework-dotnet"
:::zone-end
:::zone target="docs" pivot="framework-spring"
- Azure subscription - create one for free
- A supported Java Development Kit (JDK) with version 11.
- Apache Maven version 3.0 or above.
:::zone-end
[!INCLUDE quickstarts-free-trial-note]
To set up a managed identity in the portal, you first create an application and then enable the feature.
-
Access your App Services resource in the Azure portal. If you don't have an existing App Services resource to work with, create one.
-
Scroll down to the Settings group in the left pane, and select Identity.
-
On the System assigned tab, switch Status to On and select Save.
-
Answer Yes when prompted to enable system assigned managed identity.
The following steps describe how to assign the App Configuration Data Reader role to App Service. For detailed steps, see Assign Azure roles using the Azure portal.
-
In the Azure portal, select All resources and select the App Configuration store that you created in the quickstart.
-
Select Access control (IAM).
-
Select Add > Add role assignment.
:::image type="content" source="../../includes/role-based-access-control/media/add-role-assignment-menu-generic.png" alt-text="Screenshot showing Access control (IAM) page with Add role assignment menu open.":::
-
On the Role tab, select the App Configuration Data Reader role.
:::image type="content" source="../../includes/role-based-access-control/media/add-role-assignment-role-generic.png" alt-text="Screenshot showing Add role assignment page with Role tab selected.":::
-
On the Members tab, select Managed identity, and then select Select members.
-
Select your Azure subscription, for Managed Identity select App Service, then select your App Service name.
-
On the Review + assign tab, select Review + assign to assign the role.
:::zone target="docs" pivot="framework-dotnet"
-
Add a reference to the Azure.Identity package:
dotnet add package Azure.Identity
-
Find the endpoint to your App Configuration store. This URL is listed on the Access keys tab for the store in the Azure portal.
-
Open appsettings.json, and add the following script. Replace <service_endpoint>, including the brackets, with the URL to your App Configuration store.
"AppConfig": { "Endpoint": "<service_endpoint>" }
-
Open Program.cs, and add a reference to the
Azure.Identity
andMicrosoft.Azure.Services.AppAuthentication
namespaces:using Azure.Identity;
-
If you wish to access only values stored directly in App Configuration, update the
CreateWebHostBuilder
method by replacing theconfig.AddAzureAppConfiguration()
method (this is found in theMicrosoft.Azure.AppConfiguration.AspNetCore
package).[!IMPORTANT]
CreateHostBuilder
replacesCreateWebHostBuilder
in .NET Core 3.0. Select the correct syntax based on your environment.public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => webBuilder.ConfigureAppConfiguration((hostingContext, config) => { var settings = config.Build(); config.AddAzureAppConfiguration(options => options.Connect(new Uri(settings["AppConfig:Endpoint"]), new ManagedIdentityCredential())); }) .UseStartup<Startup>());
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => webBuilder.ConfigureAppConfiguration((hostingContext, config) => { var settings = config.Build(); config.AddAzureAppConfiguration(options => options.Connect(new Uri(settings["AppConfig:Endpoint"]), new ManagedIdentityCredential())); }) .UseStartup<Startup>());
public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .ConfigureAppConfiguration((hostingContext, config) => { var settings = config.Build(); config.AddAzureAppConfiguration(options => options.Connect(new Uri(settings["AppConfig:Endpoint"]), new ManagedIdentityCredential())); }) .UseStartup<Startup>();
[!NOTE] If you want to use a user-assigned managed identity, be sure to specify the clientId when creating the ManagedIdentityCredential.
config.AddAzureAppConfiguration(options => { options.Connect(new Uri(settings["AppConfig:Endpoint"]), new ManagedIdentityCredential("<your_clientId>")) });
As explained in the Managed Identities for Azure resources FAQs, there is a default way to resolve which managed identity is used. In this case, the Azure Identity library enforces you to specify the desired identity to avoid posible runtime issues in the future (for instance, if a new user-assigned managed identity is added or if the system-assigned managed identity is enabled). So, you will need to specify the clientId even if only one user-assigned managed identity is defined, and there is no system-assigned managed identity.
:::zone-end
:::zone target="docs" pivot="framework-spring"
-
Find the endpoint to your App Configuration store. This URL is listed on the Overview tab for the store in the Azure portal.
-
Open
bootstrap.properties
, remove the connection-string property and replace it with endpoint:
spring.cloud.azure.appconfiguration.stores[0].endpoint=<service_endpoint>
Note
If you want to use user-assigned managed identity the property spring.cloud.azure.appconfiguration.stores[0].managed-identity.client-id
, be sure to specify the clientId when creating the ManagedIdentityCredential.
:::zone-end
:::zone target="docs" pivot="framework-dotnet"
Using managed identities requires you to deploy your app to an Azure service. Managed identities can't be used for authentication of locally-running apps. To deploy the .NET Core app that you created in the Create an ASP.NET Core app with App Configuration quickstart and modified to use managed identities, follow the guidance in Publish your web app.
:::zone-end
:::zone target="docs" pivot="framework-spring"
Using managed identities requires you to deploy your app to an Azure service. Managed identities can't be used for authentication of locally-running apps. To deploy the Spring app that you created in the Create a Java Spring app with Azure App Configuration quickstart and modified to use managed identities, follow the guidance in Publish your web app.
:::zone-end
In addition to App Service, many other Azure services support managed identities. For more information, see Services that support managed identities for Azure resources.
[!INCLUDE azure-app-configuration-cleanup]
In this tutorial, you added an Azure managed identity to streamline access to App Configuration and improve credential management for your app. To learn more about how to use App Configuration, continue to the Azure CLI samples.
[!div class="nextstepaction"] CLI samples