The Error
When debugging in Visual Studio 2022:
Azure.RequestFailedException: 401 UnauthorizedVisual Studio Specific Fix - 1 Min
1. In VS2022 go to Tools → Options → Azure Service Authentication.
2. Sign in with the same Azure AD account that has access to KeyVault.
3. Restart Visual Studio and Clean + Rebuild solution.
4. Still failing? Check Output → Azure Service Authentication logs.
Quick Fix - General
1. Local dev: az login. 2. Managed Identity: Enable on App Service. 3. KeyVault Access Policy: Grant Get/List to app. 4. Code: builder.Configuration.AddAzureKeyVault(new Uri(vaultUri), new DefaultAzureCredential())
Root Cause
Visual Studio uses a different token cache than az cli. If VS is signed into wrong tenant, DefaultAzureCredential fails with 401.
Also see: Fix 401 in Docker | Original 401 Fix
No comments yet. Be the first to share your thoughts!