Getting "KeyVaultErrorException: Access denied" in Azure web app
See original GitHub issueHi!
I’m successfully retrieving a Key Vault secret in an ASP.NET Core 2 web app when running locally, but when deployed to an Azure web app, I get this:
[Critical] Microsoft.AspNetCore.Hosting.Internal.WebHost: Application startup exception
Microsoft.Azure.KeyVault.Models.KeyVaultErrorException: Access denied
at Microsoft.Azure.KeyVault.KeyVaultClient.<GetSecretsWithHttpMessagesAsync>d__66.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
at Microsoft.Azure.KeyVault.KeyVaultClientExtensions.<GetSecretsAsync>d__49.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Microsoft.Extensions.Configuration.AzureKeyVault.AzureKeyVaultConfigurationProvider.<LoadAsync>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult()
at Microsoft.Extensions.Configuration.AzureKeyVault.AzureKeyVaultConfigurationProvider.Load()
at Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(IList`1 providers)
at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()
at IdentityServer.Startup.ConfigureServices(IServiceCollection services)
In Startup.cs, I’m doing this:
public void ConfigureServices(IServiceCollection services)
{
var keyVaultClient =
new KeyVaultClient(
new KeyVaultClient.AuthenticationCallback(new AzureServiceTokenProvider().KeyVaultTokenCallback));
var configuration = new ConfigurationBuilder()
.AddEnvironmentVariables()
.AddUserSecrets<Startup>()
.AddAzureKeyVault("https://[redacted].vault.azure.net/", keyVaultClient, new DefaultKeyVaultSecretManager())
.Build();
...
I have more or less followed this: https://jeremylindsayni.wordpress.com/2018/03/19/simplifying-azure-key-vault-and-net-core-web-app-includes-nuget-package/
To be precise, I have enabled “Managed service identity”, and I have added a Key Vault access policy with the name of the app under “Select principal” as well as “Authorized application”. I have selected all secret permissions. I have tried restarting the web app.
I have not run any PowerShell command. Do I still have to?
This is with Microsoft.Azure.Services.AppAuthentication v1.1.0-preview.
Issue Analytics
- State:
- Created 5 years ago
- Comments:13 (3 by maintainers)
Top Results From Across the Web
ERROR: The user, group or application does not have ...
This error usually comes when application/user don't have permission to access the resource, Key-Vault in this case which is secured by Azure AD ......
Read more >KeyVault access 403 - Forbidden: Access is denied.
Hi Team, I have created KeyVault in Azure and assigned permission to user. (Added in access policies) He is able to access from ......
Read more >Getting "403 - Forbidden: Access is denied. You do not ...
Hi Abhay, It seems like the managed identity of your web app is unable to authenticate to Azure KeyVault. To solve this, you...
Read more >Provide access to Key Vault keys, certificates, and secrets ...
Go to key vault Access control (IAM) tab and remove "Key Vault Secrets Officer" role assignment for this resource. Remove assignment - key...
Read more >AZIdentity | Key Vault Firewall access by Azure App Services
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think I found the problem: When adding the access policy, there’s a field for “Select principal” and another for “Authorized application”. In both cases the app name can be selected, but the trick is to leave the second one blank!
This could certainly be improved with some extra guidance, and a more helpful error message.
Phew, I’ve just spent two hours of my customer’s time with figuring this one out. We certainly do need a quick fix here! Many thank @torhovland for reporting this, you’ve made my day. 😃