Skip to content

Commit 453ca93

Browse files
committed
Add Key Vault approach for client secret
1 parent 4ba62ca commit 453ca93

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

aspnetcore/blazor/security/blazor-web-app-with-entra.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,78 @@ The important changes to the `LogInOrOut` component are demonstrated in the foll
153153
</div>
154154
```
155155

156+
## Obtain the client secret from Azure Key Vault
157+
158+
[Azure Key Vault](https://azure.microsoft.com/products/key-vault/) provides a safe approach for providing the app's client secret to the app when hosting in [Microsoft Azure](https://azure.microsoft.com/).
159+
160+
To create a key vault and set a client secret, see [About Azure Key Vault secrets (Azure documentation)](/azure/key-vault/secrets/about-secrets), which cross-links resources to get started with Azure Key Vault. To implement the code in this section, record the key vault URI and the secret name from Azure when you create the key vault and secret.
161+
162+
The following `GetSecretFromKeyVault` method retrieves a secret from a key vault using the Entra tenant ID. Add this method to the server project. Adjust the namespace (`BlazorSample.Helpers`) to match your project namespace scheme.
163+
164+
`Helpers/AzureHelper`:
165+
166+
```csharp
167+
using Azure;
168+
using Azure.Identity;
169+
using Azure.Security.KeyVault.Secrets;
170+
171+
namespace BlazorSample.Helpers;
172+
173+
public static class AzureHelper
174+
{
175+
public static string GetKeyVaultSecret(string tenantId, string vaultUri, string secretName)
176+
{
177+
DefaultAzureCredentialOptions options = new()
178+
{
179+
// Specify the tenant ID to use the dev credentials when running the app locally
180+
VisualStudioTenantId = tenantId,
181+
SharedTokenCacheTenantId = tenantId
182+
};
183+
184+
var client = new SecretClient(new Uri(vaultUri), new DefaultAzureCredential(options));
185+
var secret = client.GetSecretAsync(secretName).Result;
186+
187+
return secret.Value.Value;
188+
}
189+
}
190+
```
191+
192+
In the server project's `Program` file after Microsoft identity platform services are added (`AddMicrosoftIdentityWebApp`), obtain and apply the client secret using the following code:
193+
194+
```csharp
195+
string tenantId = builder.Configuration.GetValue<string>("AzureAd:TenantId")!;
196+
string vaultUri = builder.Configuration.GetValue<string>("AzureAd:VaultUri")!;
197+
string secretName = builder.Configuration.GetValue<string>("AzureAd:SecretName")!;
198+
199+
builder.Services.Configure<MicrosoftIdentityOptions>(
200+
options =>
201+
{
202+
options.ClientSecret =
203+
AzureHelper.GetKeyVaultSecret(tenantId, vaultUri, secretName);
204+
});
205+
```
206+
207+
Supply the vault URI and secret name from configuration.
208+
209+
In the `AzureAd` section of `appsettings.json`, add configuration keys and values:
210+
211+
* The `{VAULT URI}` placeholder is the key vault URI. Include the trailing slash on the URI.
212+
* The `{SECRET NAME}` placeholder is the secret name.
213+
214+
```json
215+
"VaultUri": "{VAULT URI}",
216+
"SecretName": "{SECRET NAME}"
217+
```
218+
219+
Example:
220+
221+
```json
222+
"VaultUri": "https://contoso.vault.azure.net/",
223+
"SecretName": "BlazorSample_Entra"
224+
```
225+
226+
Configuration is used to facilitate supplying values based on the app's environmental configuration files. For example, `appsettings.Development.json` for Development, `appsettings.Staging.json` for Staging, and `appsettings.Production.json` for Production can use dedicated key vaults for each environment.
227+
156228
## Troubleshoot
157229

158230
[!INCLUDE[](~/blazor/security/includes/troubleshoot-server.md)]

0 commit comments

Comments
 (0)