Azure.Identity.CredentialUnavailableException when updating Azure.Identity from version 1.7

99 Views Asked by At

I'm trying to update my Azure function project to use a newer version of Azure.Identity than 1.7. I'm using the .net6 and v4 functions. I've tried with 1.8, 1.9, and 1.10 versions of Azure. Identity but I get the error below. Version 1.7 works fine but it has vulnerabilities so I need to update it.

enter image description here

2

There are 2 best solutions below

2
Pravallika KV On
  • You can use Azure.Identity with version 1.10 in .NET 6.0.

  • To resolve your issue, try sign out and sign in to your account in visual studio.

  • Go to Tools => Options =>Azure Service Authentication => Account Selection sign in to your account.

enter image description here I have tried using Azure.Identity package version 1.10 in my Http triggered Azure function and it worked as expected.

Function.cs:

using System;
using System.Threading.Tasks;
using Azure.Identity;
using Azure.Security.KeyVault.Secrets;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;

 public static async Task<IActionResult> Run(
     [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
     ILogger log)
 {
     string keyVaultName = Environment.GetEnvironmentVariable("<keyvault_name>");
     var kvUri = "https://<keyvault_name>.vault.azure.net/";

     var client = new SecretClient(new Uri(kvUri), new DefaultAzureCredential());
     var secretName = "kvsecret";
     var secret = await client.GetSecretAsync(secretName);

     string keyVaultValue = secret.Value.Value;
     log.LogInformation("The Secret Name: " + secretName + " and the secret value: "+ keyVaultValue);
     return new OkObjectResult("The secret value: "+keyVaultValue);
 }

.csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Azure.Identity" Version="1.10.4" />
    <PackageReference Include="Azure.Security.KeyVault.Secrets" Version="4.5.0" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.2.0" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

Output:

enter image description here

1
Heden Den Qvisten On

Seems like this is a false error. The function works as expected even though the exception is thrown.