Azure.Security.KeyVault.Certificates
4.3.0-beta.4
Prefix Reserved
See the version list below for details.
dotnet add package Azure.Security.KeyVault.Certificates --version 4.3.0-beta.4
NuGet\Install-Package Azure.Security.KeyVault.Certificates -Version 4.3.0-beta.4
<PackageReference Include="Azure.Security.KeyVault.Certificates" Version="4.3.0-beta.4" />
paket add Azure.Security.KeyVault.Certificates --version 4.3.0-beta.4
#r "nuget: Azure.Security.KeyVault.Certificates, 4.3.0-beta.4"
// Install Azure.Security.KeyVault.Certificates as a Cake Addin #addin nuget:?package=Azure.Security.KeyVault.Certificates&version=4.3.0-beta.4&prerelease // Install Azure.Security.KeyVault.Certificates as a Cake Tool #tool nuget:?package=Azure.Security.KeyVault.Certificates&version=4.3.0-beta.4&prerelease
Azure Key Vault Certificate client library for .NET
Azure Key Vault is a cloud service that provides secure storage and automated management of certificates used throughout a cloud application. Multiple certificates, and multiple versions of the same certificate, can be kept in the Azure Key Vault. Each certificate in the vault has a policy associated with it which controls the issuance and lifetime of the certificate, along with actions to be taken as certificates near expiry.
The Azure Key Vault certificates client library enables programmatically managing certificates, offering methods to create, update, list, and delete certificates, policies, issuers, and contacts. The library also supports managing pending certificate operations and management of deleted certificates.
Source code | Package (NuGet) | API reference documentation | Product documentation | Samples | Migration guide
Getting started
Install the package
Install the Azure Key Vault certificates client library for .NET with NuGet:
dotnet add package Azure.Security.KeyVault.Certificates
Prerequisites
- 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 CertificateClient 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
Output:
{ "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:
$Env:AZURE_CLIENT_ID="generated-app-ID" $Env:AZURE_CLIENT_SECRET="random-password" $Env:AZURE_TENANT_ID="tenant-ID"
Grant the above mentioned application authorization to perform certificate operations on the Azure Key Vault:
az keyvault set-policy --name <your-key-vault-name> --spn $Env:AZURE_CLIENT_ID --certificate-permissions backup delete get list create update purge
--certificate-permissions: Allowed values: backup, create, delete, deleteissuers, get, getissuers, import, list, listissuers, managecontacts, manageissuers, purge, recover, restore, setissuers, update.
If you have enabled role-based access control (RBAC) for Key Vault instead, you can find roles like "Key Vault Certificates 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 CertificateClient
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 CertificateClient:
// Create a new certificate client using the default credential from Azure.Identity using environment variables previously set,
// including AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, and AZURE_TENANT_ID.
var client = new CertificateClient(vaultUri: new Uri(vaultUrl), credential: new DefaultAzureCredential());
Key concepts
KeyVaultCertificate
A KeyVaultCertificate
is the fundamental resource within Azure Key Vault. You'll use certificates to encrypt and verify encrypted or signed data.
CertificateClient
With a CertificateClient
you can get certificates from the vault, create new certificates and
new versions of existing certificates, update certificate metadata, and delete certificates. You
can also manage certificate issuers, contacts, and management policies of certificates. This is
illustrated in the examples below.
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
Examples
The Azure.Security.KeyVault.Certificates 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 certificate service related tasks:
Sync examples
- Create a certificate
- Retrieve a certificate
- Update an existing certificate
- List certificates
- Delete a certificate
Async examples
- Create a certificate asynchronously
- List certificates asynchronously
- Delete a certificate asynchronously
Create a certificate
StartCreateCertificate
creates a certificate to be stored in the Azure Key Vault. If a certificate with
the same name already exists, then a new version of the certificate is created.
When creating the certificate the user can specify the policy which controls the certificate lifetime. If no policy is specified the default policy will be used. The StartCreateCertificate
operation returns a CertificateOperation
. The following example creates a self-signed certificate with the default policy.
// Create a certificate. This starts a long running operation to create and sign the certificate.
CertificateOperation operation = client.StartCreateCertificate("MyCertificate", CertificatePolicy.Default);
// You can await the completion of the create certificate operation.
// You should run UpdateStatus in another thread or do other work like pumping messages between calls.
while (!operation.HasCompleted)
{
Thread.Sleep(2000);
operation.UpdateStatus();
}
KeyVaultCertificateWithPolicy certificate = operation.Value;
NOTE: Depending on the certificate issuer and validation methods, certificate creation and signing can take an indeterminate amount of time. Users should only wait on certificate operations when the operation can be reasonably completed in the scope of the application, such as with self-signed certificates or issuers with well known response times.
Retrieve a certificate
GetCertificate
retrieves the latest version of a certificate stored in the Azure Key Vault along with its CertificatePolicy
.
KeyVaultCertificateWithPolicy certificateWithPolicy = client.GetCertificate("MyCertificate");
GetCertificateVersion
retrieves a specific version of a certificate in the vault.
KeyVaultCertificate certificate = client.GetCertificateVersion(certificateWithPolicy.Name, certificateWithPolicy.Properties.Version);
Update an existing certificate
UpdateCertificate
updates a certificate stored in the Azure Key Vault.
CertificateProperties certificateProperties = new CertificateProperties(certificate.Id);
certificateProperties.Tags["key1"] = "value1";
KeyVaultCertificate updated = client.UpdateCertificateProperties(certificateProperties);
List certificates
GetCertificates
enumerates the certificates in the vault, returning select properties of the
certificate. Sensitive fields of the certificate will not be returned. This operation
requires the certificates/list permission.
Pageable<CertificateProperties> allCertificates = client.GetPropertiesOfCertificates();
foreach (CertificateProperties certificateProperties in allCertificates)
{
Console.WriteLine(certificateProperties.Name);
}
Delete a certificate
DeleteCertificate
deletes all versions of a certificate stored in the Azure Key Vault. When soft-delete
is not enabled for the Azure Key Vault, this operation permanently deletes the certificate. If soft delete is enabled the certificate is marked for deletion and can be optionally purged or recovered up until its scheduled purge date.
DeleteCertificateOperation operation = client.StartDeleteCertificate("MyCertificate");
// You only need to wait for completion if you want to purge or recover the certificate.
// You should call `UpdateStatus` in another thread or after doing additional work like pumping messages.
while (!operation.HasCompleted)
{
Thread.Sleep(2000);
operation.UpdateStatus();
}
DeletedCertificate certificate = operation.Value;
client.PurgeDeletedCertificate(certificate.Name);
Create a certificate 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 certificate in the Azure Key Vault with the specified optional arguments.
// Create a certificate. This starts a long running operation to create and sign the certificate.
CertificateOperation operation = await client.StartCreateCertificateAsync("MyCertificate", CertificatePolicy.Default);
// You can await the completion of the create certificate operation.
KeyVaultCertificateWithPolicy certificate = await operation.WaitForCompletionAsync();
List certificates asynchronously
Listing certificate does not rely on awaiting the GetPropertiesOfCertificatesAsync
method, but returns an AsyncPageable<CertificateProperties>
that you can use with the await foreach
statement:
AsyncPageable<CertificateProperties> allCertificates = client.GetPropertiesOfCertificatesAsync();
await foreach (CertificateProperties certificateProperties in allCertificates)
{
Console.WriteLine(certificateProperties.Name);
}
Delete a certificate asynchronously
When deleting a certificate 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
.
DeleteCertificateOperation operation = await client.StartDeleteCertificateAsync("MyCertificate");
// You only need to wait for completion if you want to purge or recover the certificate.
await operation.WaitForCompletionAsync();
DeletedCertificate certificate = operation.Value;
await client.PurgeDeletedCertificateAsync(certificate.Name);
Troubleshooting
General
When you interact with the Azure Key Vault certificates 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 Key that doesn't exist in your Azure Key Vault, a 404
error is returned, indicating Not Found
.
try
{
KeyVaultCertificateWithPolicy certificateWithPolicy = client.GetCertificate("SomeCertificate");
}
catch (RequestFailedException ex)
{
Console.WriteLine(ex.ToString());
}
You will notice that additional information is logged, like the Client Request ID of the operation.
Message:
Azure.RequestFailedException : Service request failed.
Status: 404 (Not Found)
Content:
{"error":{"code":"CertificateNotFound","message":"Certificate not found: MyCertificate"}}
Headers:
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-service-version: 1.1.0.866
x-ms-keyvault-network-info: addr=131.107.174.199;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 certificates 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:
Sample1_HelloWorld.md - for working with Azure Key Vault certificates, including:
- Create a certificate
- Get an existing certificate
- Update an existing certificate
- Delete a certificate
Sample2_GetCertificates.md - Example code for working with Azure Key Vault certificates, including:
- Create certificates
- List all certificates in the Key Vault
- List versions of a specified certificate
- Delete certificates from the Key Vault
- List deleted certificates in the Key Vault
Additional Documentation
- For more extensive documentation on Azure Key Vault, see the API reference documentation.
- For Secrets client library see Secrets client library.
- For Keys client library see Keys client library.
Contributing
See the CONTRIBUTING.md 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 https://cla.microsoft.com.
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 opencode@microsoft.com with any additional questions or comments.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Azure.Core (>= 1.21.0)
- System.Memory (>= 4.5.4)
- System.Text.Json (>= 4.7.2)
- System.Threading.Tasks.Extensions (>= 4.5.4)
NuGet packages (59)
Showing the top 5 NuGet packages that depend on Azure.Security.KeyVault.Certificates:
Package | Downloads |
---|---|
Microsoft.Identity.Web.Certificate
This package brings certificate management for MSAL.NET. |
|
Nuke.Common
The AKEless Build System for C#/.NET Signed by signpath.io from repository 'https://github.com/nuke-build/nuke' commit 'a94fbe2ca0c12a4bf2df0a4c92e9dc51ee8f1076' (see contained AppVeyorSettings.json file for build settings). |
|
AspNetCore.HealthChecks.AzureKeyVault
HealthChecks.AzureKeyVault is the health check package for Azure Key Vault secrets |
|
Service.Extensions.KeyVault
Extensions to provide consistent configurations and patterns for your service. |
|
Apprio.Enablement.Web.Security
Package Description |
GitHub repositories (24)
Showing the top 5 popular GitHub repositories that depend on Azure.Security.KeyVault.Certificates:
Repository | Stars |
---|---|
Azure/azure-sdk-for-net
This repository is for active development of the Azure SDK for .NET. For consumers of the SDK we recommend visiting our public developer docs at https://learn.microsoft.com/dotnet/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-net.
|
|
win-acme/win-acme
A simple ACME client for Windows (for use with Let's Encrypt et al.)
|
|
Azure/azure-powershell
Microsoft Azure PowerShell
|
|
Xabaril/AspNetCore.Diagnostics.HealthChecks
Enterprise HealthChecks for ASP.NET Core Diagnostics Package
|
|
skoruba/IdentityServer4.Admin
The administration for the IdentityServer4 and Asp.Net Core Identity
|
Version | Downloads | Last updated |
---|---|---|
4.7.0 | 264,149 | 10/15/2024 |
4.6.0 | 14,212,211 | 2/15/2024 |
4.6.0-beta.2 | 7,038 | 11/13/2023 |
4.6.0-beta.1 | 196 | 11/10/2023 |
4.5.1 | 18,551,981 | 3/31/2023 |
4.5.0 | 452,168 | 3/14/2023 |
4.5.0-beta.1 | 9,854 | 11/9/2022 |
4.4.0 | 3,211,315 | 9/20/2022 |
4.3.0 | 3,970,809 | 3/26/2022 |
4.3.0-beta.4 | 18,480 | 1/13/2022 |
4.3.0-beta.2 | 12,712 | 10/14/2021 |
4.3.0-beta.1 | 176,517 | 8/11/2021 |
4.2.0 | 3,964,239 | 6/16/2021 |
4.2.0-beta.6 | 1,578 | 5/12/2021 |
4.2.0-beta.5 | 9,123 | 3/9/2021 |
4.2.0-beta.4 | 26,920 | 2/11/2021 |
4.2.0-beta.3 | 112,076 | 11/13/2020 |
4.2.0-beta.2 | 1,101 | 10/7/2020 |
4.2.0-beta.1 | 20,699 | 9/9/2020 |
4.1.0 | 63,667,005 | 8/12/2020 |
4.1.0-preview.1 | 39,012 | 3/10/2020 |
4.0.3 | 116,103 | 7/9/2020 |
4.0.2 | 2,580,099 | 4/4/2020 |
4.0.1 | 32,695 | 3/4/2020 |
4.0.0 | 115,587 | 1/9/2020 |
4.0.0-preview.8 | 576 | 12/20/2019 |
4.0.0-preview.7 | 870 | 12/4/2019 |
4.0.0-preview.6 | 716 | 11/1/2019 |
4.0.0-preview.5 | 1,280 | 10/8/2019 |
4.0.0-preview.4 | 500 | 9/11/2019 |