Unable to fetch the azure key Vault secret in .net sharepoint application

M R, MEGHA 0 Reputation points
2023-10-17T10:36:39.33+00:00

Whenever I am trying to retrieve the azure key vault secret in .net sharepoint application, I am getting the below exception for the below mentioned code . Tried to update all the nugget packages with the expected version but, still unable to troubleshoot the error.

if (!string.IsNullOrEmpty(KeyVaultName) && !string.IsNullOrEmpty(KeyVaultSecret) && !string.IsNullOrEmpty(ManagedIdentityClientId))
{
    var kvUri = $"https://{KeyVaultName}.vault.azure.net";

    var options = new DefaultAzureCredentialOptions()
    {
        ManagedIdentityClientId = ManagedIdentityClientId
    };
    var credential = new DefaultAzureCredential(options);

    var client = new SecretClient(new Uri(kvUri), credential);
    var secret = client.GetSecret(KeyVaultSecret);
   string accountPassword = secret.Value.Value;


}

Below is the exception:
System.IO.FileNotFoundException
  HResult=0x80070002
  Message=Could not load file or assembly 'Azure.Identity, Version=1.10.0.0, Culture=neutral, PublicKeyToken=92742159e12e44c8' or one of its dependencies. The system cannot find the file specified.
  Source=ACNAdHocProjectBackup
  StackTrace:
   at ACNAdHocProjectBackup.ACNPSIConfigurator.SetExternalConfigurationData() in D:\MeghaMR\Tools_MSPS2019_ACNAdHocProjectBackup\ACNAdHocProjectBackup\ACNPSIConfigurator.cs:line 453
   at ACNAdHocProjectBackup.ACNPSIConfigurator.ConfigurePSIClients() in D:\MeghaMR\Tools_MSPS2019_ACNAdHocProjectBackup\ACNAdHocProjectBackup\ACNPSIConfigurator.cs:line 102
   at ACNAdHocProjectBackup.Layouts.ACNAdHocProjectBackup.AdHocProjectBackup.configurePSI() in D:\MeghaMR\Tools_MSPS2019_ACNAdHocProjectBackup\ACNAdHocProjectBackup\Layouts\ACNAdHocProjectBackup\AdHocProjectBackup.aspx.cs:line 183
   at ACNAdHocProjectBackup.Layouts.ACNAdHocProjectBackup.AdHocProjectBackup.Page_Load(Object sender, EventArgs e) in D:\MeghaMR\Tools_MSPS2019_ACNAdHocProjectBackup\ACNAdHocProjectBackup\Layouts\ACNAdHocProjectBackup\AdHocProjectBackup.aspx.cs:line 39

  This exception was originally thrown at this call stack:
    ACNAdHocProjectBackup.ACNPSIConfigurator.ConfigurePSIClients() in ACNPSIConfigurator.cs
    ACNAdHocProjectBackup.Layouts.ACNAdHocProjectBackup.AdHocProjectBackup.configurePSI() in AdHocProjectBackup.aspx.cs
    ACNAdHocProjectBackup.Layouts.ACNAdHocProjectBackup.AdHocProjectBackup.Page_Load(object, System.EventArgs) in AdHocProjectBackup.aspx.cs
SharePoint Server
SharePoint Server
A family of Microsoft on-premises document management and storage systems.
2,243 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,438 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,793 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,354 questions
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 32,081 Reputation points Microsoft Vendor
    2023-10-18T05:52:33.74+00:00

    Hi @M R, MEGHA,

    According the error message, it seems you haven't added Azure Identity client library. You could refer to the following command to install the library azure key Vault secret needed.

    dotnet add package Azure.Security.KeyVault.Secrets
    dotnet add package Azure.Identity
    

    Here is the document for more details of the steps

    https://learn.microsoft.com/en-us/azure/key-vault/secrets/quick-create-net?tabs=azure-cli


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.