-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
121 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
sdk/keyvault/Azure.Security.KeyVault.Secrets/samples/Sample4_GetSecretIfExists.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Get a secret without throwing if it is not defined | ||
|
||
This sample demonstrates how to get a secret without throwing an exception if it is not defined. | ||
This may be useful in some application configuration managers that attempt to fetch key/value pairs from a collection of providers and when exceptions may break startup or are otherwise costly. | ||
To better configure ASP.NET applications or other applications that use common [.NET Configuration], see our documentation on [Azure.Extensions.AspNetCore.Configuration.Secrets]. | ||
To get started, you'll need a URI to an Azure Key Vault. See the [README](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/keyvault/Azure.Security.KeyVault.Secrets/README.md) for links and instructions. | ||
|
||
## Creating a SecretClient | ||
|
||
To create a new `SecretClient` to create get a secret, you need the endpoint to an Azure Key Vault and credentials. | ||
You can use the [DefaultAzureCredential] to try a number of common authentication methods optimized for both running as a service and development. | ||
|
||
In the sample below, you can set `keyVaultUrl` based on an environment variable, configuration setting, or any way that works for your application. | ||
|
||
```C# Snippet:SecretsSample4SecretClient | ||
var client = new SecretClient(new Uri(keyVaultUrl), new DefaultAzureCredential()); | ||
``` | ||
|
||
## Get a secret | ||
|
||
To prevent throwing a `RequestFailedException` when a secret does not exist, or to alter other behaviors of a specific API call, you can create a `RequestContext` and pass that to the API call: | ||
|
||
```C# Snippet:SecretsSample4GetSecretIfExists | ||
// Do not treat HTTP 404 responses as errors. | ||
RequestContext context = new RequestContext(); | ||
context.AddClassifier(404, false); | ||
|
||
// Try getting the latest application connection string using the context above. | ||
NullableResponse<KeyVaultSecret> response = client.GetSecret("appConnectionString", null, context); | ||
if (response.HasValue) | ||
{ | ||
KeyVaultSecret secret = response.Value; | ||
Debug.WriteLine($"Secret is returned with name {secret.Name} and value {secret.Value}"); | ||
} | ||
``` | ||
|
||
You can also do this asynchronously: | ||
|
||
```C# Snippet:SecretsSample4GetSecretIfExistsAsync | ||
// Do not treat HTTP 404 responses as errors. | ||
RequestContext context = new RequestContext(); | ||
context.AddClassifier(404, false); | ||
|
||
// Try getting the latest application connection string using the context above. | ||
NullableResponse<KeyVaultSecret> response = await client.GetSecretAsync("appConnectionString", null, context); | ||
if (response.HasValue) | ||
{ | ||
KeyVaultSecret secret = response.Value; | ||
Debug.WriteLine($"Secret is returned with name {secret.Name} and value {secret.Value}"); | ||
} | ||
``` | ||
|
||
[.NET Configuration]: https://learn.microsoft.com/dotnet/core/extensions/configuration | ||
[Azure.Extensions.AspNetCore.Configuration.Secrets]: https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/extensions/Azure.Extensions.AspNetCore.Configuration.Secrets/README.md | ||
[DefaultAzureCredential]: https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/identity/Azure.Identity/README.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
sdk/keyvault/Azure.Security.KeyVault.Secrets/tests/samples/Sample4_GetSecretIfExists.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.Threading.Tasks; | ||
using NUnit.Framework; | ||
|
||
namespace Azure.Security.KeyVault.Secrets.Samples | ||
{ | ||
public partial class GetSecretIfExists | ||
{ | ||
[Test] | ||
public void GetSecretIfExistsSync() | ||
{ | ||
// Environment variable with the Key Vault endpoint. | ||
string keyVaultUrl = TestEnvironment.KeyVaultUrl; | ||
|
||
#region Snippet:SecretsSample4SecretClient | ||
var client = new SecretClient(new Uri(keyVaultUrl), new DefaultAzureCredential()); | ||
#endregion | ||
|
||
#region Snippet:SecretsSample4GetSecretIfExists | ||
// Do not treat HTTP 404 responses as errors. | ||
RequestContext context = new RequestContext(); | ||
context.AddClassifier(404, false); | ||
|
||
// Try getting the latest application connection string using the context above. | ||
NullableResponse<KeyVaultSecret> response = client.GetSecret("appConnectionString", null, context); | ||
if (response.HasValue) | ||
{ | ||
KeyVaultSecret secret = response.Value; | ||
Debug.WriteLine($"Secret is returned with name {secret.Name} and value {secret.Value}"); | ||
} | ||
#endregion | ||
} | ||
|
||
[Test] | ||
public async Task GetSecretIfExistsAsync() | ||
{ | ||
// Environment variable with the Key Vault endpoint. | ||
string keyVaultUrl = TestEnvironment.KeyVaultUrl; | ||
|
||
var client = new SecretClient(new Uri(keyVaultUrl), new DefaultAzureCredential()); | ||
|
||
#region Snippet:SecretsSample4GetSecretIfExistsAsync | ||
// Do not treat HTTP 404 responses as errors. | ||
RequestContext context = new RequestContext(); | ||
context.AddClassifier(404, false); | ||
|
||
// Try getting the latest application connection string using the context above. | ||
NullableResponse<KeyVaultSecret> response = await client.GetSecretAsync("appConnectionString", null, context); | ||
if (response.HasValue) | ||
{ | ||
KeyVaultSecret secret = response.Value; | ||
Debug.WriteLine($"Secret is returned with name {secret.Name} and value {secret.Value}"); | ||
} | ||
#endregion | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters