Skip to content

Commit c1f3267

Browse files
committed
WASM+Identity acct conf and PW recovery
1 parent 6f15b09 commit c1f3267

15 files changed

+688
-35
lines changed

.openpublishing.redirection.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,6 +1312,16 @@
13121312
"source_path": "aspnetcore/blazor/images.md",
13131313
"redirect_url": "/aspnet/core/blazor/images-and-documents",
13141314
"redirect_document_id": false
1315+
},
1316+
{
1317+
"source_path": "aspnetcore/blazor/security/webassembly/standalone-with-identity.md",
1318+
"redirect_url": "/aspnet/core/blazor/security/webassembly/standalone-with-identity/",
1319+
"redirect_document_id": false
1320+
},
1321+
{
1322+
"source_path": "blazor/security/server/account-confirmation-and-password-recovery.md",
1323+
"redirect_url": "/aspnet/core/blazor/security/account-confirmation-and-password-recovery",
1324+
"redirect_document_id": false
13151325
}
13161326
]
13171327
}

aspnetcore/blazor/call-web-api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ builder.Services.AddHttpClient(...)
686686

687687
:::moniker range=">= aspnetcore-8.0"
688688

689-
For a demonstration, see <xref:blazor/security/webassembly/standalone-with-identity>.
689+
For a demonstration, see <xref:blazor/security/webassembly/standalone-with-identity/index>.
690690

691691
:::moniker-end
692692

aspnetcore/blazor/fundamentals/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ Samples apps in the repository:
189189
* Two Blazor Web Apps and a Blazor WebAssembly app for calling web (server) APIs (<xref:blazor/call-web-api>)
190190
* Blazor Web App with OIDC (BFF and non-BFF patterns) (<xref:blazor/security/blazor-web-app-oidc>)
191191
* Blazor WebAssembly scopes-enabled logging (<xref:blazor/fundamentals/logging#client-side-log-scopes>)
192-
* Blazor WebAssembly with ASP.NET Core Identity (<xref:blazor/security/webassembly/standalone-with-identity>)
192+
* Blazor WebAssembly with ASP.NET Core Identity (<xref:blazor/security/webassembly/standalone-with-identity/index>)
193193
* .NET MAUI Blazor Hybrid app with a Blazor Web App and a shared UI provided by a Razor class library (RCL) (<xref:blazor/hybrid/tutorials/maui-blazor-web-app>)
194194

195195
:::moniker-end

aspnetcore/blazor/security/server/account-confirmation-and-password-recovery.md renamed to aspnetcore/blazor/security/account-confirmation-and-password-recovery.md

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ description: Learn how to configure an ASP.NET Core Blazor Web App with email co
55
ms.author: riande
66
monikerRange: '>= aspnetcore-8.0'
77
ms.date: 02/09/2024
8-
uid: blazor/security/server/account-confirmation-and-password-recovery
8+
uid: blazor/security/account-confirmation-and-password-recovery
99
---
1010
# Account confirmation and password recovery in ASP.NET Core Blazor
1111

1212
This article explains how to configure an ASP.NET Core Blazor Web App with email confirmation and password recovery.
1313

14+
> [!NOTE]
15+
> This article only applies to Blazor Web Apps. To implement email confirmation and password recovery for standalone Blazor WebAssembly apps with ASP.NET Core Identity, see <xref:blazor/security/webassembly/standalone-with-identity/account-confirmation-and-password-recovery>.
16+
1417
## Namespace
1518

1619
The app's namespace used by the example in this article is `BlazorSample`. Update the code examples to use the namespace of your app.
@@ -21,7 +24,7 @@ In this article, [Mailchimp's Transactional API](https://mailchimp.com/developer
2124

2225
Create a class to fetch the secure email API key. The example in this article uses a class named `AuthMessageSenderOptions` with a `EmailAuthKey` property to hold the key.
2326

24-
`AuthMessageSenderOptions`:
27+
`AuthMessageSenderOptions.cs`:
2528

2629
```csharp
2730
namespace BlazorSample;
@@ -40,19 +43,31 @@ builder.Services.Configure<AuthMessageSenderOptions>(builder.Configuration);
4043

4144
## Configure a user secret for the provider's security key
4245

43-
Set the key with the [Secret Manager tool](xref:security/app-secrets). In the following example, the key name is `EmailAuthKey`, and the key is represented by the `{KEY}` placeholder. In a command shell, navigate to the app's root folder and execute the following command with the API key:
46+
If the project has already been initialized for the [Secret Manager tool](xref:security/app-secrets), it will already have an app secrets identifier (`<AppSecretsId>`) in its project file (`.csproj`). In Visual Studio, you can tell if the app secrets ID is present by looking at the **Properties** panel when the project is selected in **Solution Explorer**. If the app hasn't been initialized, execute the following command in a command shell opened to the project's directory. In Visual Studio, you can use the Developer PowerShell command prompt.
47+
48+
```dotnetcli
49+
dotnet user-secrets init
50+
```
51+
52+
Set the key with the Secret Manager tool. In the following example, the key name is `EmailAuthKey`, and the key is represented by the `{KEY}` placeholder. In a command shell, navigate to the app's root folder and execute the following command with the API key:
4453

4554
```dotnetcli
4655
dotnet user-secrets set "EmailAuthKey" "{KEY}"
4756
```
4857

58+
If using Visual Studio, you can confirm the secret is set by right-clicking the server project in **Solution Explorer** and selecting **Manage User Secrets**.
59+
4960
For more information, see <xref:security/app-secrets>.
5061

5162
[!INCLUDE[](~/blazor/security/includes/secure-authentication-flows.md)]
5263

5364
## Implement `IEmailSender`
5465

55-
Implement `IEmailSender` for the provider. The following example is based on Mailchimp's Transactional API using [Mandrill.net](https://www.nuget.org/packages/Mandrill.net). For a different provider, refer to their documentation on how to implement sending a message in the `Execute` method.
66+
The following example is based on Mailchimp's Transactional API using [Mandrill.net](https://www.nuget.org/packages/Mandrill.net). For a different provider, refer to their documentation on how to implement sending an email message.
67+
68+
Add the [Mandrill.net](https://www.nuget.org/packages/Mandrill.net) NuGet package to the project.
69+
70+
Add the following `EmailSender` class to implement <xref:Microsoft.AspNetCore.Identity.IEmailSender>.
5671

5772
`Components/Account/EmailSender.cs`:
5873

@@ -170,7 +185,7 @@ builder.Services.Configure<DataProtectionTokenProviderOptions>(options =>
170185
options.TokenLifespan = TimeSpan.FromHours(3));
171186
```
172187

173-
The built in Identity user tokens ([AspNetCore/src/Identity/Extensions.Core/src/TokenOptions.cs](https://github.com/dotnet/aspnetcore/blob/main/src/Identity/Extensions.Core/src/TokenOptions.cs)) have a [one day timeout](https://github.com/dotnet/AspNetCore/blob/main/src/Identity/Core/src/DataProtectionTokenProviderOptions.cs).
188+
The built-in Identity user tokens ([AspNetCore/src/Identity/Extensions.Core/src/TokenOptions.cs](https://github.com/dotnet/aspnetcore/blob/main/src/Identity/Extensions.Core/src/TokenOptions.cs)) have a [one day timeout](https://github.com/dotnet/AspNetCore/blob/main/src/Identity/Core/src/DataProtectionTokenProviderOptions.cs).
174189

175190
[!INCLUDE[](~/includes/aspnetcore-repo-ref-source-links.md)]
176191

@@ -256,6 +271,13 @@ builder.Services
256271
.AddTransient<CustomEmailConfirmationTokenProvider<ApplicationUser>>();
257272
```
258273

274+
## Enable account confirmation after a site has users
275+
276+
Enabling account confirmation on a site with users locks out all the existing users. Existing users are locked out because their accounts aren't confirmed. To work around existing user lockout, use one of the following approaches:
277+
278+
* Update the database to mark all existing users as confirmed.
279+
* Confirm existing users. For example, batch-send emails with confirmation links.
280+
259281
## Troubleshoot
260282

261283
If you can't get email working:
@@ -267,16 +289,6 @@ If you can't get email working:
267289
* Try another email alias on a different email provider, such as Microsoft, Yahoo, or Gmail.
268290
* Try sending to different email accounts.
269291

270-
> [!WARNING]
271-
> Do **not** use production secrets in test and development. If you publish the app to Azure, set secrets as application settings in the Azure Web App portal. The configuration system is set up to read keys from environment variables.
272-
273-
## Enable account confirmation after a site has users
274-
275-
Enabling account confirmation on a site with users locks out all the existing users. Existing users are locked out because their accounts aren't confirmed. To work around existing user lockout, use one of the following approaches:
276-
277-
* Update the database to mark all existing users as confirmed.
278-
* Confirm existing users. For example, batch-send emails with confirmation links.
279-
280292
## Additional resources
281293

282294
* [Mandrill.net (GitHub repository)](https://github.com/feinoujc/Mandrill.net)

aspnetcore/blazor/security/authentication-state.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ The following component's `SignIn` method creates a claims principal for the use
474474
* [Server-side unauthorized content display while prerendering with a custom `AuthenticationStateProvider`](xref:blazor/security/server/index#unauthorized-content-display-while-prerendering-with-a-custom-authenticationstateprovider)
475475
* [How to access an `AuthenticationStateProvider` from a `DelegatingHandler` set up using an `IHttpClientFactory`](xref:blazor/security/server/additional-scenarios#access-authenticationstateprovider-in-outgoing-request-middleware)
476476
* <xref:blazor/security/blazor-web-app-oidc>
477-
* <xref:blazor/security/webassembly/standalone-with-identity>
477+
* <xref:blazor/security/webassembly/standalone-with-identity/index>
478478

479479
:::moniker-end
480480

@@ -483,7 +483,7 @@ The following component's `SignIn` method creates a claims principal for the use
483483
* [Server-side unauthorized content display while prerendering with a custom `AuthenticationStateProvider`](xref:blazor/security/server/index#unauthorized-content-display-while-prerendering-with-a-custom-authenticationstateprovider)
484484
* [How to access an `AuthenticationStateProvider` from a `DelegatingHandler` set up using an `IHttpClientFactory`](xref:blazor/security/server/additional-scenarios#access-authenticationstateprovider-in-outgoing-request-middleware)
485485
* <xref:blazor/security/blazor-web-app-oidc>
486-
* <xref:blazor/security/webassembly/standalone-with-identity>
486+
* <xref:blazor/security/webassembly/standalone-with-identity/index>
487487
[Prerendering with authentication in hosted Blazor WebAssembly apps](xref:blazor/security/webassembly/additional-scenarios#prerendering-with-authentication)
488488

489489
:::moniker-end

0 commit comments

Comments
 (0)