Azure.Security.KeyVault.Secrets 4.4.0

The ID prefix of this package has been reserved for one of the owners of this package by Prefix Reserved
.NET Standard 2.0
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package Azure.Security.KeyVault.Secrets --version 4.4.0
NuGet\Install-Package Azure.Security.KeyVault.Secrets -Version 4.4.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Azure.Security.KeyVault.Secrets" Version="4.4.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Azure.Security.KeyVault.Secrets --version 4.4.0
#r "nuget: Azure.Security.KeyVault.Secrets, 4.4.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Azure.Security.KeyVault.Secrets as a Cake Addin
#addin nuget:?package=Azure.Security.KeyVault.Secrets&version=4.4.0

// Install Azure.Security.KeyVault.Secrets as a Cake Tool
#tool nuget:?package=Azure.Security.KeyVault.Secrets&version=4.4.0

Azure Key Vault secret client library for .NET

Azure Key Vault is a cloud service that provides a secure storage of secrets, such as passwords and database connection strings.

The Azure Key Vault secrets client library allows you to securely store and control the access to tokens, passwords, API keys, and other secrets. This library offers operations to create, retrieve, update, delete, purge, backup, restore, and list the secrets and its versions.

Source code | Package (NuGet) | API reference documentation | Product documentation | Samples | Migration guide

Getting started

Install the package

Install the Azure Key Vault secrets client library for .NET with NuGet:

dotnet add package Azure.Security.KeyVault.Secrets


  • An Azure subscription.
  • An existing Azure Key Vault. If you need to create an Azure Key Vault, you can use the Azure Portal or Azure CLI.

If you use the Azure CLI, replace <your-resource-group-name> and <your-key-vault-name> with your own, unique names:

az keyvault create --resource-group <your-resource-group-name> --name <your-key-vault-name>

Authenticate the client

In order to interact with the Azure Key Vault service, you'll need to create an instance of the SecretClient class. You need a vault url, which you may see as "DNS Name" in the portal, and client secret credentials (client id, client secret, tenant id) to instantiate a client object.

Client secret credential authentication is being used in this getting started section but you can find more ways to authenticate with Azure identity. To use the DefaultAzureCredential provider shown below, or other credential providers provided with the Azure SDK, you should install the Azure.Identity package:

dotnet add package Azure.Identity
Create/Get credentials

Use the Azure CLI snippet below to create/get client secret credentials.

  • Create a service principal and configure its access to Azure resources:

    az ad sp create-for-rbac -n <your-application-name> --skip-assignment


        "appId": "generated-app-ID",
        "displayName": "dummy-app-name",
        "name": "http://dummy-app-name",
        "password": "random-password",
        "tenant": "tenant-ID"
  • Use the returned credentials above to set AZURE_CLIENT_ID (appId), AZURE_CLIENT_SECRET (password), and AZURE_TENANT_ID (tenant) environment variables. The following example shows a way to do this in Powershell:

  • Grant the above mentioned application authorization to perform secret operations on the Azure Key Vault:

    az keyvault set-policy --name <your-key-vault-name> --spn $Env:AZURE_CLIENT_ID --secret-permissions backup delete get list set

    --secret-permissions: Accepted values: backup, delete, get, list, purge, recover, restore, set

    If you have enabled role-based access control (RBAC) for Key Vault instead, you can find roles like "Key Vault Secrets Officer" in our RBAC guide.

  • Use the above mentioned Azure Key Vault name to retrieve details of your Vault which also contains your Azure Key Vault URL:

    az keyvault show --name <your-key-vault-name> --query properties.vaultUri --output tsv
Create SecretClient

Once you've populated the AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, and AZURE_TENANT_ID environment variables, replace vaultUrl with the output of az keyvault show in the example below to create the SecretClient:

// Create a new secret client using the default credential from Azure.Identity using environment variables previously set,
var client = new SecretClient(vaultUri: new Uri(vaultUrl), credential: new DefaultAzureCredential());

// Create a new secret using the secret client.
KeyVaultSecret secret = client.SetSecret("secret-name", "secret-value");

// Retrieve a secret using the secret client.
secret = client.GetSecret("secret-name");

Key concepts


A KeyVaultSecret is the fundamental resource within Azure Key Vault. From a developer's perspective, Azure Key Vault APIs accept and return secret values as strings.


A SecretClient provides both synchronous and asynchronous operations in the SDK allowing for selection of a client based on an application's use case. Once you've initialized a SecretClient, you can interact with secrets in Azure Key Vault.

Thread safety

We guarantee that all client instance methods are thread-safe and independent of each other (guideline). This ensures that the recommendation of reusing client instances is always safe, even across threads.

Additional concepts

Client options | Accessing the response | Long-running operations | Handling failures | Diagnostics | Mocking | Client lifetime


The Azure.Security.KeyVault.Secrets package supports synchronous and asynchronous APIs.

The following section provides several code snippets using the client created above, covering some of the most common Azure Key Vault secret service related tasks:

Sync examples

Async examples

Create a secret

SetSecret creates a KeyVaultSecret to be stored in the Azure Key Vault. If a secret with the same name already exists, then a new version of the secret is created.

KeyVaultSecret secret = client.SetSecret("secret-name", "secret-value");


Retrieve a secret

GetSecret retrieves a secret previously stored in the Azure Key Vault.

KeyVaultSecret secret = client.GetSecret("secret-name");


Update an existing secret

UpdateSecretProperties updates a secret previously stored in the Azure Key Vault. Only the attributes of the secret are updated. To update the value, call SecretClient.SetSecret on a secret with the same name.

KeyVaultSecret secret = client.GetSecret("secret-name");

// Clients may specify the content type of a secret to assist in interpreting the secret data when its retrieved.
secret.Properties.ContentType = "text/plain";

// You can specify additional application-specific metadata in the form of tags.
secret.Properties.Tags["foo"] = "updated tag";

SecretProperties updatedSecretProperties = client.UpdateSecretProperties(secret.Properties);


Delete a secret

StartDeleteSecret starts a long-running operation to delete a secret previously stored in the Azure Key Vault. You can retrieve the secret immediately without waiting for the operation to complete. When soft-delete is not enabled for the Azure Key Vault, this operation permanently deletes the secret.

DeleteSecretOperation operation = client.StartDeleteSecret("secret-name");

DeletedSecret secret = operation.Value;

Delete and purge a secret

You will need to wait for the long-running operation to complete before trying to purge or recover the secret. You can do this by calling UpdateStatus in a loop as shown below:

DeleteSecretOperation operation = client.StartDeleteSecret("secret-name");

// You only need to wait for completion if you want to purge or recover the secret.
// You should call `UpdateStatus` in another thread or after doing additional work like pumping messages.
while (!operation.HasCompleted)


DeletedSecret secret = operation.Value;

List secrets

This example lists all the secrets in the specified Azure Key Vault. The value is not returned when listing all secrets. You will need to call SecretClient.GetSecret to retrieve the value.

Pageable<SecretProperties> allSecrets = client.GetPropertiesOfSecrets();

foreach (SecretProperties secretProperties in allSecrets)

Create a secret asynchronously

The asynchronous APIs are identical to their synchronous counterparts, but return with the typical "Async" suffix for asynchronous methods and return a Task.

This example creates a secret in the Azure Key Vault with the specified optional arguments.

KeyVaultSecret secret = await client.SetSecretAsync("secret-name", "secret-value");


List secrets asynchronously

Listing secrets does not rely on awaiting the GetPropertiesOfSecretsAsync method, but returns an AsyncPageable<SecretProperties> that you can use with the await foreach statement:

AsyncPageable<SecretProperties> allSecrets = client.GetPropertiesOfSecretsAsync();

await foreach (SecretProperties secretProperties in allSecrets)

Delete a secret asynchronously

When deleting a secret asynchronously before you purge it, you can await the WaitForCompletionAsync method on the operation. By default, this loops indefinitely but you can cancel it by passing a CancellationToken.

DeleteSecretOperation operation = await client.StartDeleteSecretAsync("secret-name");

// You only need to wait for completion if you want to purge or recover the secret.
await operation.WaitForCompletionAsync();

DeletedSecret secret = operation.Value;
await client.PurgeDeletedSecretAsync(secret.Name);


See our troubleshooting guide for details on how to diagnose various failure scenarios.


When you interact with the Azure Key Vault secret client library using the .NET SDK, errors returned by the service correspond to the same HTTP status codes returned for REST API requests.

For example, if you try to retrieve a secret that doesn't exist in your Azure Key Vault, a 404 error is returned, indicating Not Found.

    KeyVaultSecret secret = client.GetSecret("some_secret");
catch (RequestFailedException ex)

You will notice that additional information is logged, like the Client Request ID of the operation.

    Azure.RequestFailedException : Service request failed.
    Status: 404 (Not Found) 
    {"error":{"code":"SecretNotFound","message":"Secret not found: some_secret"}}
    Cache-Control: no-cache
    Pragma: no-cache
    Server: Microsoft-IIS/10.0
    x-ms-keyvault-region: westus
    x-ms-request-id: 625f870e-10ea-41e5-8380-282e5cf768f2
    x-ms-keyvault-network-info: addr=;act_addr_fam=InterNetwork;
    X-AspNet-Version: 4.0.30319
    X-Powered-By: ASP.NET
    Strict-Transport-Security: max-age=31536000;includeSubDomains
    X-Content-Type-Options: nosniff
    Date: Tue, 18 Jun 2019 16:02:11 GMT
    Content-Length: 75
    Content-Type: application/json; charset=utf-8
    Expires: -1

Next steps

Several Azure Key Vault secrets client library samples are available to you in this GitHub repository. These samples provide example code for additional scenarios commonly encountered while working with Azure Key Vault:

  • - for working with Azure Key Vault, including:

    • Create a secret
    • Get an existing secret
    • Update an existing secret
    • Delete secret
  • - contains the code snippets working with Azure Key Vault secrets, including:

    • Backup and recover a secret
  • - example code for working with Azure Key Vault secrets, including:

    • Create secrets
    • List all secrets in the Key Vault
    • Update secrets in the Key Vault
    • List versions of a specified secret
    • Delete secrets from the Key Vault
    • List deleted secrets in the Key Vault

Additional Documentation


See the for details on building, testing, and contributing to these libraries.

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact with any additional questions or comments.

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48 net481
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (127)

Showing the top 5 NuGet packages that depend on Azure.Security.KeyVault.Secrets:

Package Downloads
Microsoft.Extensions.Configuration.AzureAppConfiguration The ID prefix of this package has been reserved for one of the owners of this package by

Microsoft.Extensions.Configuration.AzureAppConfiguration is a configuration provider for the .NET Core framework that allows developers to use Microsoft Azure App Configuration service as a configuration source in their applications.

Azure.Extensions.AspNetCore.Configuration.Secrets The ID prefix of this package has been reserved for one of the owners of this package by

Azure Key Vault configuration provider implementation for Microsoft.Extensions.Configuration.

Microsoft.Identity.Web.Certificate The ID prefix of this package has been reserved for one of the owners of this package by

This package brings certificate management for MSAL.NET.


HealthChecks.AzureKeyVault is the health check package for Azure Key Vault secrets

Microsoft.Configuration.ConfigurationBuilders.Azure The ID prefix of this package has been reserved for one of the owners of this package by

A set of Configuration Builders for the .Net Framework that draw from Azure resources.

GitHub repositories (25)

Showing the top 5 popular GitHub repositories that depend on Azure.Security.KeyVault.Secrets:

Repository Stars
Cloud Native application framework for .NET
Enterprise HealthChecks for ASP.NET Core Diagnostics Package
.NET language playground
The host/runtime that powers Azure Functions
Blazor Boilerplate / Starter Template with MudBlazor
Version Downloads Last updated
4.5.0-beta.1 9,946 11/9/2022
4.4.0 2,447,966 9/20/2022
4.3.0 6,518,944 3/26/2022
4.3.0-beta.4 64,328 1/13/2022
4.3.0-beta.2 56,987 10/14/2021
4.3.0-beta.1 211,063 8/11/2021
4.2.0 15,657,234 6/16/2021
4.2.0-beta.5 25,909 5/12/2021
4.2.0-beta.4 125,868 2/11/2021
4.2.0-beta.3 151,500 11/13/2020
4.2.0-beta.2 8,775 10/7/2020
4.2.0-beta.1 22,615 9/9/2020
4.1.0 29,706,850 8/12/2020
4.1.0-preview.1 78,323 3/10/2020
4.0.3 2,530,140 4/4/2020
4.0.2 7,256,619 3/4/2020
4.0.1 19,567,622 1/9/2020
4.0.0 957,177 10/31/2019
4.0.0-preview.5 1,492 10/8/2019
4.0.0-preview.4 786 9/11/2019
4.0.0-preview.3 2,991 8/9/2019
4.0.0-preview.2 320 8/6/2019
4.0.0-preview.1 1,181 6/28/2019