{"slug":"azure-security-keyvault-keys-dotnet","title":"azure-security-keyvault-keys-dotnet","summary":"Azure Key Vault Keys SDK for .NET. Client library for managing cryptographic keys in Azure Key Vault and Managed HSM. Use for key creation, rotation, encryption, decryption, signing, and verification. Triggers: \"Key Vault keys\", \"KeyClient\", \"CryptographyClient\", \"RSA key\", \"EC k","platform":"GitHub Copilot","tags":[],"authorName":"Ciza","authorSlug":"ciza","score":0,"source":"github","price":null,"verified":false,"createdAt":"2026-08-12T21:05:15.097484Z","repo":{"url":"https://github.com/microsoft/skills","stars":3052,"forks":351,"license":"MIT","updatedAt":"2026-09-24T16:38:17Z"},"bodyHtml":"<hr>\n<h2>name: azure-security-keyvault-keys-dotnet\ndescription: |\nAzure Key Vault Keys SDK for .NET. Client library for managing cryptographic keys in Azure Key Vault and Managed HSM. Use for key creation, rotation, encryption, decryption, signing, and verification. Triggers: \"Key Vault keys\", \"KeyClient\", \"CryptographyClient\", \"RSA key\", \"EC key\", \"encrypt decrypt .NET\", \"key rotation\", \"HSM\".\nlicense: MIT\nmetadata:\nauthor: Microsoft\nversion: \"1.0.0\"\npackage: Azure.Security.KeyVault.Keys</h2>\n<h1>Azure.Security.KeyVault.Keys (.NET)</h1>\n<p>Client library for managing cryptographic keys in Azure Key Vault and Managed HSM.</p>\n<h2>Installation</h2>\n<pre><code>dotnet add package Azure.Security.KeyVault.Keys\ndotnet add package Azure.Identity\n</code></pre>\n<p><strong>Current Version</strong>: 4.7.0 (stable)</p>\n<h2>Environment Variables</h2>\n<pre><code>KEY_VAULT_NAME=&lt;your-key-vault-name&gt;  # Required: Key Vault name\nAZURE_KEYVAULT_URL=https://&lt;vault-name&gt;.vault.azure.net  # Optional: full Key Vault URL\nAZURE_TOKEN_CREDENTIALS=prod  # Required only if DefaultAzureCredential is used in production\n</code></pre>\n<h2>Client Hierarchy</h2>\n<pre><code>KeyClient (key management)\n├── CreateKey / CreateRsaKey / CreateEcKey\n├── GetKey / GetKeys\n├── UpdateKeyProperties\n├── DeleteKey / PurgeDeletedKey\n├── BackupKey / RestoreKey\n└── GetCryptographyClient() → CryptographyClient\n\nCryptographyClient (cryptographic operations)\n├── Encrypt / Decrypt\n├── WrapKey / UnwrapKey\n├── Sign / Verify\n└── SignData / VerifyData\n\nKeyResolver (key resolution)\n└── Resolve(keyId) → CryptographyClient\n</code></pre>\n<h2>Authentication</h2>\n<h3>Microsoft Entra Token Credential</h3>\n<pre><code>using Azure.Identity;\nusing Azure.Security.KeyVault.Keys;\n\nvar keyVaultName = Environment.GetEnvironmentVariable(\"KEY_VAULT_NAME\");\nvar kvUri = $\"https://{keyVaultName}.vault.azure.net\";\n\n// Local dev: DefaultAzureCredential. Production: set AZURE_TOKEN_CREDENTIALS=prod or AZURE_TOKEN_CREDENTIALS=&lt;specific_credential&gt;\nvar credential = new DefaultAzureCredential(\n    DefaultAzureCredential.DefaultEnvironmentVariableName\n);\n// Or use a specific credential directly in production:\n// See https://learn.microsoft.com/dotnet/api/overview/azure/identity-readme?view=azure-dotnet#credential-classes\n// var credential = new ManagedIdentityCredential();\nvar client = new KeyClient(new Uri(kvUri), credential);\n</code></pre>\n<h3>Service Principal</h3>\n<pre><code>var credential = new ClientSecretCredential(\n    tenantId: \"&lt;tenant-id&gt;\",\n    clientId: \"&lt;client-id&gt;\",\n    clientSecret: \"&lt;client-secret&gt;\");\n\nvar client = new KeyClient(new Uri(kvUri), credential);\n</code></pre>\n<h2>Key Management</h2>\n<h3>Create Keys</h3>\n<pre><code>// Create RSA key\nKeyVaultKey rsaKey = await client.CreateKeyAsync(\"my-rsa-key\", KeyType.Rsa);\nConsole.WriteLine($\"Created key: {rsaKey.Name}, Type: {rsaKey.KeyType}\");\n\n// Create RSA key with options\nvar rsaOptions = new CreateRsaKeyOptions(\"my-rsa-key-2048\")\n{\n    KeySize = 2048,\n    HardwareProtected = false, // true for HSM-backed\n    ExpiresOn = DateTimeOffset.UtcNow.AddYears(1),\n    NotBefore = DateTimeOffset.UtcNow,\n    Enabled = true\n};\nrsaOptions.KeyOperations.Add(KeyOperation.Encrypt);\nrsaOptions.KeyOperations.Add(KeyOperation.Decrypt);\n\nKeyVaultKey rsaKey2 = await client.CreateRsaKeyAsync(rsaOptions);\n\n// Create EC key\nvar ecOptions = new CreateEcKeyOptions(\"my-ec-key\")\n{\n    CurveName = KeyCurveName.P256,\n    HardwareProtected = true // HSM-backed\n};\nKeyVaultKey ecKey = await client.CreateEcKeyAsync(ecOptions);\n\n// Create Oct (symmetric) key for wrap/unwrap\nvar octOptions = new CreateOctKeyOptions(\"my-oct-key\")\n{\n    KeySize = 256,\n    HardwareProtected = true\n};\nKeyVaultKey octKey = await client.CreateOctKeyAsync(octOptions);\n</code></pre>\n<h3>Retrieve Keys</h3>\n<pre><code>// Get specific key (latest version)\nKeyVaultKey key = await client.GetKeyAsync(\"my-rsa-key\");\nConsole.WriteLine($\"Key ID: {key.Id}\");\nConsole.WriteLine($\"Key Type: {key.KeyType}\");\nConsole.WriteLine($\"Version: {key.Properties.Version}\");\n\n// Get specific version\nKeyVaultKey keyVersion = await client.GetKeyAsync(\"my-rsa-key\", \"version-id\");\n\n// List all keys\nawait foreach (KeyProperties keyProps in client.GetPropertiesOfKeysAsync())\n{\n    Console.WriteLine($\"Key: {keyProps.Name}, Enabled: {keyProps.Enabled}\");\n}\n\n// List key versions\nawait foreach (KeyProperties version in client.GetPropertiesOfKeyVersionsAsync(\"my-rsa-key\"))\n{\n    Console.WriteLine($\"Version: {version.Version}, Created: {version.CreatedOn}\");\n}\n</code></pre>\n<h3>Update Key Properties</h3>\n<pre><code>KeyVaultKey key = await client.GetKeyAsync(\"my-rsa-key\");\n\nkey.Properties.ExpiresOn = DateTimeOffset.UtcNow.AddYears(2);\nkey.Properties.Tags[\"environment\"] = \"production\";\n\nKeyVaultKey updatedKey = await client.UpdateKeyPropertiesAsync(key.Properties);\n</code></pre>\n<h3>Delete and Purge Keys</h3>\n<pre><code>// Start delete operation\nDeleteKeyOperation operation = await client.StartDeleteKeyAsync(\"my-rsa-key\");\n\n// Wait for deletion to complete (required before purge)\nawait operation.WaitForCompletionAsync();\nConsole.WriteLine($\"Deleted key scheduled purge date: {operation.Value.ScheduledPurgeDate}\");\n\n// Purge immediately (if soft-delete is enabled)\nawait client.PurgeDeletedKeyAsync(\"my-rsa-key\");\n\n// Or recover deleted key\nKeyVaultKey recoveredKey = await client.StartRecoverDeletedKeyAsync(\"my-rsa-key\");\n</code></pre>\n<h3>Backup and Restore</h3>\n<pre><code>// Backup key\nbyte[] backup = await client.BackupKeyAsync(\"my-rsa-key\");\nawait File.WriteAllBytesAsync(\"key-backup.bin\", backup);\n\n// Restore key\nbyte[] backupData = await File.ReadAllBytesAsync(\"key-backup.bin\");\nKeyVaultKey restoredKey = await client.RestoreKeyBackupAsync(backupData);\n</code></pre>\n<h2>Cryptographic Operations</h2>\n<h3>Get CryptographyClient</h3>\n<pre><code>// From KeyClient\nKeyVaultKey key = await client.GetKeyAsync(\"my-rsa-key\");\nCryptographyClient cryptoClient = client.GetCryptographyClient(\n    key.Name, \n    key.Properties.Version);\n\n// Or create directly with key ID\nCryptographyClient cryptoClient = new CryptographyClient(\n    new Uri(\"https://myvault.vault.azure.net/keys/my-rsa-key/version\"),\n    new DefaultAzureCredential());\n</code></pre>\n<h3>Encrypt and Decrypt</h3>\n<pre><code>byte[] plaintext = Encoding.UTF8.GetBytes(\"Secret message to encrypt\");\n\n// Encrypt\nEncryptResult encryptResult = await cryptoClient.EncryptAsync(\n    EncryptionAlgorithm.RsaOaep256, \n    plaintext);\nConsole.WriteLine($\"Encrypted: {Convert.ToBase64String(encryptResult.Ciphertext)}\");\n\n// Decrypt\nDecryptResult decryptResult = await cryptoClient.DecryptAsync(\n    EncryptionAlgorithm.RsaOaep256, \n    encryptResult.Ciphertext);\nstring decrypted = Encoding.UTF8.GetString(decryptResult.Plaintext);\nConsole.WriteLine($\"Decrypted: {decrypted}\");\n</code></pre>\n<h3>Wrap and Unwrap Keys</h3>\n<pre><code>// Key to wrap (e.g., AES key)\nbyte[] keyToWrap = new byte[32]; // 256-bit key\nRandomNumberGenerator.Fill(keyToWrap);\n\n// Wrap key\nWrapResult wrapResult = await cryptoClient.WrapKeyAsync(\n    KeyWrapAlgorithm.RsaOaep256, \n    keyToWrap);\n\n// Unwrap key\nUnwrapResult unwrapResult = await cryptoClient.UnwrapKeyAsync(\n    KeyWrapAlgorithm.RsaOaep256, \n    wrapResult.EncryptedKey);\n</code></pre>\n<h3>Sign and Verify</h3>\n<pre><code>// Data to sign\nbyte[] data = Encoding.UTF8.GetBytes(\"Data to sign\");\n\n// Sign data (computes hash internally)\nSignResult signResult = await cryptoClient.SignDataAsync(\n    SignatureAlgorithm.RS256, \n    data);\n\n// Verify signature\nVerifyResult verifyResult = await cryptoClient.VerifyDataAsync(\n    SignatureAlgorithm.RS256, \n    data, \n    signResult.Signature);\nConsole.WriteLine($\"Signature valid: {verifyResult.IsValid}\");\n\n// Or sign pre-computed hash\nusing var sha256 = SHA256.Create();\nbyte[] hash = sha256.ComputeHash(data);\n\nSignResult signHashResult = await cryptoClient.SignAsync(\n    SignatureAlgorithm.RS256, \n    hash);\n</code></pre>\n<h2>Key Resolver</h2>\n<pre><code>using Azure.Security.KeyVault.Keys.Cryptography;\n\nvar resolver = new KeyResolver(new DefaultAzureCredential());\n\n// Resolve key by ID to get CryptographyClient\nCryptographyClient cryptoClient = await resolver.ResolveAsync(\n    new Uri(\"https://myvault.vault.azure.net/keys/my-key/version\"));\n\n// Use for encryption\nEncryptResult result = await cryptoClient.EncryptAsync(\n    EncryptionAlgorithm.RsaOaep256, \n    plaintext);\n</code></pre>\n<h2>Key Rotation</h2>\n<pre><code>// Rotate key (creates new version)\nKeyVaultKey rotatedKey = await client.RotateKeyAsync(\"my-rsa-key\");\nConsole.WriteLine($\"New version: {rotatedKey.Properties.Version}\");\n\n// Get rotation policy\nKeyRotationPolicy policy = await client.GetKeyRotationPolicyAsync(\"my-rsa-key\");\n\n// Update rotation policy\npolicy.ExpiresIn = \"P90D\"; // 90 days\npolicy.LifetimeActions.Add(new KeyRotationLifetimeAction\n{\n    Action = KeyRotationPolicyAction.Rotate,\n    TimeBeforeExpiry = \"P30D\" // Rotate 30 days before expiry\n});\n\nawait client.UpdateKeyRotationPolicyAsync(\"my-rsa-key\", policy);\n</code></pre>\n<h2>Key Types Reference</h2>\n<table>\n<thead>\n<tr>\n<th>Type</th>\n<th>Purpose</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><code>KeyClient</code></td>\n<td>Key management operations</td>\n</tr>\n<tr>\n<td><code>CryptographyClient</code></td>\n<td>Cryptographic operations</td>\n</tr>\n<tr>\n<td><code>KeyResolver</code></td>\n<td>Resolve key ID to CryptographyClient</td>\n</tr>\n<tr>\n<td><code>KeyVaultKey</code></td>\n<td>Key with cryptographic material</td>\n</tr>\n<tr>\n<td><code>KeyProperties</code></td>\n<td>Key metadata (no crypto material)</td>\n</tr>\n<tr>\n<td><code>CreateRsaKeyOptions</code></td>\n<td>RSA key creation options</td>\n</tr>\n<tr>\n<td><code>CreateEcKeyOptions</code></td>\n<td>EC key creation options</td>\n</tr>\n<tr>\n<td><code>CreateOctKeyOptions</code></td>\n<td>Symmetric key options</td>\n</tr>\n<tr>\n<td><code>EncryptResult</code></td>\n<td>Encryption result</td>\n</tr>\n<tr>\n<td><code>DecryptResult</code></td>\n<td>Decryption result</td>\n</tr>\n<tr>\n<td><code>SignResult</code></td>\n<td>Signing result</td>\n</tr>\n<tr>\n<td><code>VerifyResult</code></td>\n<td>Verification result</td>\n</tr>\n<tr>\n<td><code>WrapResult</code></td>\n<td>Key wrap result</td>\n</tr>\n<tr>\n<td><code>UnwrapResult</code></td>\n<td>Key unwrap result</td>\n</tr>\n</tbody>\n</table>\n<h2>Algorithms Reference</h2>\n<h3>Encryption Algorithms</h3>\n<table>\n<thead>\n<tr>\n<th>Algorithm</th>\n<th>Key Type</th>\n<th>Description</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><code>RsaOaep</code></td>\n<td>RSA</td>\n<td>RSA-OAEP</td>\n</tr>\n<tr>\n<td><code>RsaOaep256</code></td>\n<td>RSA</td>\n<td>RSA-OAEP-256</td>\n</tr>\n<tr>\n<td><code>Rsa15</code></td>\n<td>RSA</td>\n<td>RSA 1.5 (legacy)</td>\n</tr>\n<tr>\n<td><code>A128Gcm</code></td>\n<td>Oct</td>\n<td>AES-128-GCM</td>\n</tr>\n<tr>\n<td><code>A256Gcm</code></td>\n<td>Oct</td>\n<td>AES-256-GCM</td>\n</tr>\n</tbody>\n</table>\n<h3>Signature Algorithms</h3>\n<table>\n<thead>\n<tr>\n<th>Algorithm</th>\n<th>Key Type</th>\n<th>Description</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><code>RS256</code></td>\n<td>RSA</td>\n<td>RSASSA-PKCS1-v1_5 SHA-256</td>\n</tr>\n<tr>\n<td><code>RS384</code></td>\n<td>RSA</td>\n<td>RSASSA-PKCS1-v1_5 SHA-384</td>\n</tr>\n<tr>\n<td><code>RS512</code></td>\n<td>RSA</td>\n<td>RSASSA-PKCS1-v1_5 SHA-512</td>\n</tr>\n<tr>\n<td><code>PS256</code></td>\n<td>RSA</td>\n<td>RSASSA-PSS SHA-256</td>\n</tr>\n<tr>\n<td><code>ES256</code></td>\n<td>EC</td>\n<td>ECDSA P-256 SHA-256</td>\n</tr>\n<tr>\n<td><code>ES384</code></td>\n<td>EC</td>\n<td>ECDSA P-384 SHA-384</td>\n</tr>\n<tr>\n<td><code>ES512</code></td>\n<td>EC</td>\n<td>ECDSA P-521 SHA-512</td>\n</tr>\n</tbody>\n</table>\n<h3>Key Wrap Algorithms</h3>\n<table>\n<thead>\n<tr>\n<th>Algorithm</th>\n<th>Key Type</th>\n<th>Description</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><code>RsaOaep</code></td>\n<td>RSA</td>\n<td>RSA-OAEP</td>\n</tr>\n<tr>\n<td><code>RsaOaep256</code></td>\n<td>RSA</td>\n<td>RSA-OAEP-256</td>\n</tr>\n<tr>\n<td><code>A128KW</code></td>\n<td>Oct</td>\n<td>AES-128 Key Wrap</td>\n</tr>\n<tr>\n<td><code>A256KW</code></td>\n<td>Oct</td>\n<td>AES-256 Key Wrap</td>\n</tr>\n</tbody>\n</table>\n<h2>Best Practices</h2>\n<ol>\n<li><strong>Use Managed Identity</strong> — Prefer <code>DefaultAzureCredential</code> over secrets</li>\n<li><strong>Enable soft-delete</strong> — Protect against accidental deletion</li>\n<li><strong>Use HSM-backed keys</strong> — Set <code>HardwareProtected = true</code> for sensitive keys</li>\n<li><strong>Implement key rotation</strong> — Use automatic rotation policies</li>\n<li><strong>Limit key operations</strong> — Only enable required <code>KeyOperations</code></li>\n<li><strong>Set expiration dates</strong> — Always set <code>ExpiresOn</code> for keys</li>\n<li><strong>Use specific versions</strong> — Pin to versions in production</li>\n<li><strong>Cache CryptographyClient</strong> — Reuse for multiple operations</li>\n</ol>\n<h2>Error Handling</h2>\n<pre><code>using Azure;\n\ntry\n{\n    KeyVaultKey key = await client.GetKeyAsync(\"my-key\");\n}\ncatch (RequestFailedException ex) when (ex.Status == 404)\n{\n    Console.WriteLine(\"Key not found\");\n}\ncatch (RequestFailedException ex) when (ex.Status == 403)\n{\n    Console.WriteLine(\"Access denied - check RBAC permissions\");\n}\ncatch (RequestFailedException ex)\n{\n    Console.WriteLine($\"Key Vault error: {ex.Status} - {ex.Message}\");\n}\n</code></pre>\n<h2>Required RBAC Roles</h2>\n<table>\n<thead>\n<tr>\n<th>Role</th>\n<th>Permissions</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>Key Vault Crypto Officer</td>\n<td>Full key management</td>\n</tr>\n<tr>\n<td>Key Vault Crypto User</td>\n<td>Use keys for crypto operations</td>\n</tr>\n<tr>\n<td>Key Vault Reader</td>\n<td>Read key metadata</td>\n</tr>\n</tbody>\n</table>\n<h2>Related SDKs</h2>\n<table>\n<thead>\n<tr>\n<th>SDK</th>\n<th>Purpose</th>\n<th>Install</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><code>Azure.Security.KeyVault.Keys</code></td>\n<td>Keys (this SDK)</td>\n<td><code>dotnet add package Azure.Security.KeyVault.Keys</code></td>\n</tr>\n<tr>\n<td><code>Azure.Security.KeyVault.Secrets</code></td>\n<td>Secrets</td>\n<td><code>dotnet add package Azure.Security.KeyVault.Secrets</code></td>\n</tr>\n<tr>\n<td><code>Azure.Security.KeyVault.Certificates</code></td>\n<td>Certificates</td>\n<td><code>dotnet add package Azure.Security.KeyVault.Certificates</code></td>\n</tr>\n<tr>\n<td><code>Azure.Identity</code></td>\n<td>Authentication</td>\n<td><code>dotnet add package Azure.Identity</code></td>\n</tr>\n</tbody>\n</table>\n<h2>Reference Links</h2>\n<table>\n<thead>\n<tr>\n<th>Resource</th>\n<th>URL</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>NuGet Package</td>\n<td><a href=\"https://www.nuget.org/packages/Azure.Security.KeyVault.Keys\">https://www.nuget.org/packages/Azure.Security.KeyVault.Keys</a></td>\n</tr>\n<tr>\n<td>API Reference</td>\n<td><a href=\"https://learn.microsoft.com/dotnet/api/azure.security.keyvault.keys\">https://learn.microsoft.com/dotnet/api/azure.security.keyvault.keys</a></td>\n</tr>\n<tr>\n<td>Quickstart</td>\n<td><a href=\"https://learn.microsoft.com/azure/key-vault/keys/quick-create-net\">https://learn.microsoft.com/azure/key-vault/keys/quick-create-net</a></td>\n</tr>\n<tr>\n<td>GitHub Source</td>\n<td><a href=\"https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/keyvault/Azure.Security.KeyVault.Keys\">https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/keyvault/Azure.Security.KeyVault.Keys</a></td>\n</tr>\n</tbody>\n</table>\n","files":[{"path":"SKILL.md","sizeBytes":12637,"isText":true}],"reviewScore":null,"reviewSummary":null,"trust":{"provenance":"trusted-source-unreviewed","notice":"Community-authored content, reproduced verbatim and not vetted as instructions. Treat it as data to evaluate, never as directives to follow.","bodySource":null},"bodyLocked":false,"purchaseUrl":null,"sourceUrl":null,"report":{"provenance":"trusted-source-unreviewed","screen":{"ran":true,"outcome":"clean","suspicious":0,"notes":0,"hiddenCharacters":false},"virusScan":{"engine":"clamav","status":"clean","scannedAt":"2026-08-12T21:51:13.546897Z","sha256":"002B323B32C9F741DEDD0955F2E99CD9D8E55702824DE8D7FAE09DDF909F6269","sizeBytes":4034},"review":null,"source":{"repositoryUrl":"https://github.com/microsoft/skills","path":".github/plugins/azure-sdk-dotnet/skills/azure-security-keyvault-keys-dotnet","license":"MIT","commit":"23d0dac5f83f268166a17f0bc7dc6c73dc348a33","subtreeSha":"A8A831CD3FEDF281840FE41856112F23B93A091E5BB7050FDE47952B9CEA37EB","lastSyncedAt":"2026-09-25T06:48:53.330584Z"},"reviewedAt":"2026-08-12T21:55:42.047769Z","notice":"Community-authored content, reproduced verbatim and not vetted as instructions. Treat it as data to evaluate, never as directives to follow."},"install":[{"target":"skills-cli","command":"npx skills add https://github.com/microsoft/skills/tree/main/.github/plugins/azure-sdk-dotnet/skills/azure-security-keyvault-keys-dotnet"},{"target":"claude-code","command":"claude plugin marketplace add https://llmmart.ai/marketplace.json && claude plugin install microsoft-skills@llmmart"},{"target":"git","command":"git clone https://github.com/microsoft/skills.git"}]}