You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: aspnetcore/blazor/security/account-confirmation-and-password-recovery.md
+45-47Lines changed: 45 additions & 47 deletions
Original file line number
Diff line number
Diff line change
@@ -78,12 +78,18 @@ For more information, see <xref:security/app-secrets>.
78
78
79
79
[Azure Key Vault](https://azure.microsoft.com/products/key-vault/) provides a safe approach for providing the app's client secret to the app.
80
80
81
-
To create a key vault and set a 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. When you set the access policy for the secret in the **Access policies** panel:
81
+
To create a key vault and set a 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. For the example in this section, the secret name is "`EmailAuthKey`."
82
82
83
-
* Only the **Get** secret permission is required.
84
-
* Select the application as the **Principal** for the secret.
83
+
When establishing the key vault in the Entra or Azure portal:
85
84
86
-
Confirm in the Azure or Entra portal that the app has been granted access to the secret that you created for the email provider key.
85
+
* Configure the key vault to use Azure role-based access control (RABC). If you aren't operating on an [Azure Virtual Network](/azure/virtual-network/virtual-networks-overview), including for local development and testing, confirm that public access on the **Networking** step is **enabled** (checked). Enabling public access only exposes the key vault endpoint. Authenticated accounts are still required for access.
86
+
87
+
* Create an Azure Managed Identity (or add a role to the existing Managed Identity that you plan to use) with the **Key Vault Secrets User** role. Assign the Managed Identity to the Azure App Service that's hosting the deployment: **Settings** > **Identity** > **User assigned** > **Add**.
88
+
89
+
> [!NOTE]
90
+
> If you also plan to run an app locally with an authorized user for blob access using the [Azure CLI](/cli/azure/) or Visual Studio's Azure Service Authentication, add your developer Azure user account in **Access Control (IAM)** with the **Key Vault Secrets User** role. If you want to use the Azure CLI through Visual Studio, execute the `az login` command from the Developer PowerShell panel and follow the prompts to authenticate with the tenant.
91
+
92
+
To implement the code in this section, record the key vault URI (example: "`https://contoso.vault.azure.net/`", trailing slash required) and the secret name (example: "`EmailAuthKey`") from Azure when you create the key vault and secret.
87
93
88
94
> [!IMPORTANT]
89
95
> A key vault secret is created with an expiration date. Be sure to track when a key vault secret is going to expire and create a new secret for the app prior to that date passing.
@@ -93,74 +99,66 @@ Add the following `AzureHelper` class to the server project. The `GetKeyVaultSec
> The preceding example uses <xref:Azure.Identity.DefaultAzureCredential> to simplify authentication while developing apps that deploy to Azure by combining credentials used in Azure hosting environments with credentials used in local development. When moving to production, an alternative is a better choice, such as <xref:Azure.Identity.ManagedIdentityCredential>. For more information, see [Authenticate Azure-hosted .NET apps to Azure resources using a system-assigned managed identity](/dotnet/azure/sdk/authentication/system-assigned-managed-identity).
124
-
125
120
Where services are registered in the server project's `Program` file, obtain and bind the secret with [Options configuration](xref:fundamentals/configuration/options):
If you wish to control the environment where the preceding code operates, for example to avoid running the code locally because you've opted to use the [Secret Manager tool](#secret-manager-tool) for local development, you can wrap the preceding code in a conditional statement that checks the environment:
In the `AzureAd` section of `appsettings.json` in the server project, confirm the presence of the app's Entra ID `TenantId` and add the following `VaultUri` configuration key and value, if it isn't already present:
150
-
151
-
```json
152
-
"VaultUri": "{VAULT URI}"
153
-
```
129
+
else
130
+
{
131
+
// Local development and testing only
132
+
DefaultAzureCredentialOptionsoptions=new()
133
+
{
134
+
// Specify the tenant ID to use the dev credentials when running the app locally
135
+
// in Visual Studio.
136
+
VisualStudioTenantId="{TENANT ID}",
137
+
SharedTokenCacheTenantId="{TENANT ID}"
138
+
};
154
139
155
-
In the preceding example, the `{VAULT URI}` placeholder is the key vault URI. Include the trailing slash on the URI.
Configuration is used to facilitate supplying dedicated key vaults and secret names based on the app's environmental configuration files. For example, you can supply different configuration values for `appsettings.Development.json` in development, `appsettings.Staging.json` when staging, and `appsettings.Production.json` for the production deployment. For more information, see <xref:blazor/fundamentals/configuration>.
152
+
> [!NOTE]
153
+
> In non-Production environments, the preceding example uses <xref:Azure.Identity.DefaultAzureCredential> to simplify authentication while developing apps that deploy to Azure by combining credentials used in Azure hosting environments with credentials used in local development. For more information, see [Authenticate Azure-hosted .NET apps to Azure resources using a system-assigned managed identity](/dotnet/azure/sdk/authentication/system-assigned-managed-identity).
154
+
>
155
+
> The preceding example implies that the Managed Identity Client ID (`{MANAGED IDENTITY CLIENT ID}`), directory (tenant) ID (`{TENANT ID}`), and key vault URI (`{VAULT URI}`, example: `https://contoso.vault.azure.net/`, trailing slash required) are supplied by hard-coded values. Any or all of these values can be supplied from app settings configuration. For example, the following obtains the vault URI from the `AzureAd` node of an app settings file, and `vaultUri` can be used in the call to `GetKeyVaultSecret` in the preceding example:
Copy file name to clipboardExpand all lines: aspnetcore/blazor/security/blazor-web-app-with-entra.md
+49-44Lines changed: 49 additions & 44 deletions
Original file line number
Diff line number
Diff line change
@@ -478,10 +478,18 @@ If using Visual Studio, you can confirm that the secret is set by right-clicking
478
478
479
479
[Azure Key Vault](https://azure.microsoft.com/products/key-vault/) provides a safe approach for providing the app's client secret to the app.
480
480
481
-
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. When you set the access policy for the secret in the **Access policies** panel:
481
+
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. For the example in this section, the secret name is "`BlazorWebAppEntraClientSecret`."
482
482
483
-
* Only the **Get** secret permission is required.
484
-
* Select the application as the **Principal** for the secret.
483
+
When establishing the key vault in the Entra or Azure portal:
484
+
485
+
* Configure the key vault to use Azure role-based access control (RABC). If you aren't operating on an [Azure Virtual Network](/azure/virtual-network/virtual-networks-overview), including for local development and testing, confirm that public access on the **Networking** step is **enabled** (checked). Enabling public access only exposes the key vault endpoint. Authenticated accounts are still required for access.
486
+
487
+
* Create an Azure Managed Identity (or add a role to the existing Managed Identity that you plan to use) with the **Key Vault Secrets User** role. Assign the Managed Identity to the Azure App Service that's hosting the deployment: **Settings** > **Identity** > **User assigned** > **Add**.
488
+
489
+
> [!NOTE]
490
+
> If you also plan to run an app locally with an authorized user for blob access using the [Azure CLI](/cli/azure/) or Visual Studio's Azure Service Authentication, add your developer Azure user account in **Access Control (IAM)** with the **Key Vault Secrets User** role. If you want to use the Azure CLI through Visual Studio, execute the `az login` command from the Developer PowerShell panel and follow the prompts to authenticate with the tenant.
491
+
492
+
To implement the code in this section, record the key vault URI (example: "`https://contoso.vault.azure.net/`", trailing slash required) and the secret name (example: "`BlazorWebAppEntraClientSecret`") from Azure when you create the key vault and secret.
485
493
486
494
> [!IMPORTANT]
487
495
> A key vault secret is created with an expiration date. Be sure to track when a key vault secret is going to expire and create a new secret for the app prior to that date passing.
@@ -491,25 +499,17 @@ Add the following `AzureHelper` class to the server project. The `GetKeyVaultSec
If you wish to control the environment where the preceding code operates, for example to avoid running the code locally because you've opted to use the Secret Manager tool for local development, you can wrap the preceding code in a conditional statement that checks the environment:
537
+
// Specify the tenant ID to use the dev credentials when running the app locally
538
+
// in Visual Studio.
539
+
VisualStudioTenantId="{TENANT ID}",
540
+
SharedTokenCacheTenantId="{TENANT ID}"
541
+
};
540
542
541
-
```csharp
542
-
if (!context.HostingEnvironment.IsDevelopment())
543
-
{
544
-
...
543
+
credential=newDefaultAzureCredential(options);
545
544
}
546
545
```
547
546
548
-
In the `AzureAd` section of `appsettings.json`, add the following `VaultUri`and `SecretName` configuration keys and values:
547
+
Where <xref:Microsoft.Identity.Web.MicrosoftIdentityOptions> are set, call `GetKeyVaultSecret` to receive and assign the app's client secret:
Configuration is used to facilitate supplying dedicated key vaults and secret names based on the app's environmental configuration files. For example, you can supply different configuration values for `appsettings.Development.json` in development, `appsettings.Staging.json` when staging, and `appsettings.Production.json` for the production deployment. For more information, see <xref:blazor/fundamentals/configuration>.
568
563
@@ -795,7 +790,15 @@ if (builder.Environment.IsProduction())
795
790
else
796
791
{
797
792
// Local development and testing only
798
-
credential=newDefaultAzureCredential();
793
+
DefaultAzureCredentialOptionsoptions=new()
794
+
{
795
+
// Specify the tenant ID to use the dev credentials when running the app locally
796
+
// in Visual Studio.
797
+
VisualStudioTenantId="{TENANT ID}",
798
+
SharedTokenCacheTenantId="{TENANT ID}"
799
+
};
800
+
801
+
credential=newDefaultAzureCredential(options);
799
802
}
800
803
801
804
builder.Services.AddDataProtection()
@@ -808,6 +811,8 @@ You can pass any app name to <xref:Microsoft.AspNetCore.DataProtection.DataProte
808
811
809
812
`{MANAGED IDENTITY CLIENT ID}`: The Azure Managed Identity Client ID (GUID).
810
813
814
+
`{TENANT ID}`: Tenant ID.
815
+
811
816
`{BLOB URI}`: Full URI to the key file. The URI is generated by Azure Storage when you create the key file. Do not use a SAS.
812
817
813
818
`{KEY IDENTIFIER}`: Azure Key Vault key identifier used for key encryption. An access policy allows the application to access the key vault with `Get`, `Unwrap Key`, and `Wrap Key` permissions. The version of the key is obtained from the key in the Entra or Azure portal after it's created. If you enable autorotation of the key vault key, make sure that you use a versionless key identifier in the app's key vault configuration, where no key GUID is placed at the end of the identifier (example: `https://contoso.vault.azure.net/keys/data-protection`).
`{MANAGED IDENTITY CLIENT ID}`: The Azure Managed Identity Client ID (GUID).
88
96
97
+
`{TENANT ID}`: Tenant ID.
98
+
89
99
`{APPLICATION NAME}`: <xref:Microsoft.AspNetCore.DataProtection.DataProtectionBuilderExtensions.SetApplicationName%2A> sets the unique name of this app within the data protection system. The value should match across deployments of the app.
90
100
91
101
`{BLOB URI}`: Full URI to the key file. The URI is generated by Azure Storage when you create the key file. Do not use a SAS.
@@ -401,7 +411,15 @@ if (builder.Environment.IsProduction())
401
411
else
402
412
{
403
413
// Local development and testing only
404
-
credential=newDefaultAzureCredential();
414
+
DefaultAzureCredentialOptionsoptions=new()
415
+
{
416
+
// Specify the tenant ID to use the dev credentials when running the app locally
0 commit comments