Skip to content

Commit e4863ac

Browse files
authored
Blazor WASM remote auth path option config (#37012)
1 parent c066e68 commit e4863ac

9 files changed

+63
-28
lines changed
Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,8 @@
11
The `RedirectToLogin` component (`RedirectToLogin.razor`):
22

33
* Manages redirecting unauthorized users to the login page.
4-
* The current URL that the user is attempting to access is maintained by so that they can be returned to that page if authentication is successful using:
4+
* The current URL that the user is attempting to access is maintained so that they can be returned to that page if authentication is successful using:
55
* [Navigation history state](xref:blazor/fundamentals/navigation#navigation-history-state) in ASP.NET Core in .NET 7 or later.
66
* A query string in ASP.NET Core in .NET 6 or earlier.
77

88
Inspect the `RedirectToLogin` component in [reference source](https://github.com/dotnet/aspnetcore/tree/main/src/ProjectTemplates/Web.ProjectTemplates/content/ComponentsWebAssembly-CSharp). The location of the component changed over time, so use GitHub search tools to locate the component.
9-
10-
The login path can be customized by the app (<xref:Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationApplicationPathsOptions.LogInPath%2A?displayProperty=nameWithType>, [framework defaults (`dotnet/aspnetcore` reference source)](https://github.com/dotnet/aspnetcore/blob/main/src/Components/WebAssembly/WebAssembly.Authentication/src/RemoteAuthenticationDefaults.cs)). The project template's `RedirectToLogin` component uses the default login path of `authentication/login`.
11-
12-
[!INCLUDE[](~/includes/aspnetcore-repo-ref-source-links.md)]
13-
14-
If an app [customizes the login path](xref:blazor/security/webassembly/additional-scenarios#customize-app-routes), take either of the following approaches:
15-
16-
* Match the path in the hard-coded string in the `RedirectToLogin` component.
17-
* Inject <xref:Microsoft.AspNetCore.Builder.RemoteAuthenticationOptions> to obtain the configured value. For example, take this approach when you customize the path with <xref:Microsoft.Extensions.DependencyInjection.WebAssemblyAuthenticationServiceCollectionExtensions.AddApiAuthorization%2A>.
18-
Add the following directives at the top of the `RedirectToLogin` component:
19-
20-
```razor
21-
@using Microsoft.Extensions.Options
22-
@inject IOptionsSnapshot<RemoteAuthenticationOptions<ApiAuthorizationProviderOptions>> RemoteOptions
23-
```
24-
25-
Modify the component's redirect in the `OnInitialized` method:
26-
27-
```diff
28-
- Navigation.NavigateToLogin("authentication/login");
29-
+ Navigation.NavigateToLogin(RemoteOptions.Get(Options.DefaultName)
30-
+ .AuthenticationPaths.LogInPath);
31-
```
32-
33-
> [!NOTE]
34-
> If other paths differ from the project template's paths or [framework's default paths](https://github.com/dotnet/aspnetcore/blob/main/src/Components/WebAssembly/WebAssembly.Authentication/src/RemoteAuthenticationDefaults.cs), they should managed in the same fashion.
35-
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Remote authentication paths are customized using <xref:Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationApplicationPathsOptions> on the <xref:Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationOptions%601.AuthenticationPaths%2A?displayProperty=nameWithType> property in the app's `Program` file. For the framework's default path values, see the [`dotnet/aspnetcore` reference source](https://github.com/dotnet/aspnetcore/blob/main/src/Components/WebAssembly/WebAssembly.Authentication/src/RemoteAuthenticationDefaults.cs).
2+
3+
[!INCLUDE[](~/includes/aspnetcore-repo-ref-source-links.md)]
4+
5+
If an app [customizes a remote authentication path](xref:blazor/security/webassembly/additional-scenarios#customize-app-routes), take either of the following approaches:
6+
7+
* Match the path in hard-coded strings around the app.
8+
9+
* Inject <xref:Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationOptions%601?displayProperty=nameWithType> to obtain the configured value around the app. The following example demonstrates the approach for the [`RedirectToLogin` component](#redirecttologin-component).
10+
11+
Add the following Razor directives to the top of the component's Razor file:
12+
13+
```razor
14+
@using Microsoft.Extensions.Options
15+
@inject IOptionsSnapshot<RemoteAuthenticationOptions<ApiAuthorizationProviderOptions>> RemoteOptions
16+
```
17+
18+
Modify the component's redirect in the `OnInitialized` method:
19+
20+
```diff
21+
- Navigation.NavigateToLogin("authentication/login");
22+
+ Navigation.NavigateToLogin(RemoteOptions.Get(Options.DefaultName)
23+
+ .AuthenticationPaths.LogInPath);
24+
```
25+
26+
> [!NOTE]
27+
> If other paths differ from the project template's paths or [framework's default paths](https://github.com/dotnet/aspnetcore/blob/main/src/Components/WebAssembly/WebAssembly.Authentication/src/RemoteAuthenticationDefaults.cs), manage them in the same fashion.

aspnetcore/blazor/security/webassembly/hosted-with-azure-active-directory-b2c.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,12 @@ To configure the app to receive the value from the `name` claim type:
161161
});
162162
```
163163

164+
## Remote authentication paths
165+
166+
*This section pertains to the solution's **:::no-loc text="Client":::** app.*
167+
168+
[!INCLUDE[](~/blazor/security/includes/remote-authentication-paths.md)]
169+
164170
## Parts of the solution
165171

166172
This section describes the parts of a solution generated from the Blazor WebAssembly project template and describes how the solution's **:::no-loc text="Client":::** and **:::no-loc text="Server":::** projects are configured for reference. There's no specific guidance to follow in this section for a basic working application if you created the app using the guidance in the [Walkthrough](#walkthrough) section. The guidance in this section is helpful for updating an app to authenticate and authorize users. However, an alternative approach to updating an app is to create a new app from the guidance in the [Walkthrough](#walkthrough) section and moving the app's components, classes, and resources to the new app.

aspnetcore/blazor/security/webassembly/hosted-with-identity-server.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ For more information, see the [`dotnet new`](/dotnet/core/tools/dotnet-new) comm
7777

7878
[!INCLUDE[](~/blazor/security/includes/run-the-app.md)]
7979

80+
## Remote authentication paths
81+
82+
*This section pertains to the solution's **:::no-loc text="Client":::** app.*
83+
84+
[!INCLUDE[](~/blazor/security/includes/remote-authentication-paths.md)]
85+
8086
## Parts of the solution
8187

8288
This section describes the parts of a solution generated from the Blazor WebAssembly project template and describes how the solution's **:::no-loc text="Client":::** and **:::no-loc text="Server":::** projects are configured for reference. There's no specific guidance to follow in this section for a basic working application if you created the app using the guidance in the [Walkthrough](#walkthrough) section. The guidance in this section is helpful for updating an app to authenticate and authorize users. However, an alternative approach to updating an app is to create a new app from the guidance in the [Walkthrough](#walkthrough) section and moving the app's components, classes, and resources to the new app.

aspnetcore/blazor/security/webassembly/hosted-with-microsoft-entra-id.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,12 @@ To configure the app to receive the value from the `name` claim type:
165165
});
166166
```
167167

168+
## Remote authentication paths
169+
170+
*This section pertains to the solution's **:::no-loc text="Client":::** app.*
171+
172+
[!INCLUDE[](~/blazor/security/includes/remote-authentication-paths.md)]
173+
168174
## Parts of the solution
169175

170176
This section describes the parts of a solution generated from the Blazor WebAssembly project template and describes how the solution's **:::no-loc text="Client":::** and **:::no-loc text="Server":::** projects are configured for reference. There's no specific guidance to follow in this section for a basic working application if you created the app using the guidance in the [Walkthrough](#walkthrough) section. The guidance in this section is helpful for updating an app to authenticate and authorize users. However, an alternative approach to updating an app is to create a new app from the guidance in the [Walkthrough](#walkthrough) section and moving the app's components, classes, and resources to the new app.

aspnetcore/blazor/security/webassembly/standalone-with-authentication-library.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ Use one of the following approaches to run the app:
123123
* Press <kbd>F5</kbd>.
124124
* .NET CLI command shell: Execute the `dotnet watch` (or `dotnet run`) command from the app's folder.
125125

126+
## Remote authentication paths
127+
128+
[!INCLUDE[](~/blazor/security/includes/remote-authentication-paths.md)]
129+
126130
## Parts of the app
127131

128132
This section describes the parts of an app generated from the Blazor WebAssembly project template and how the app is configured. There's no specific guidance to follow in this section for a basic working application if you created the app using the guidance in the [Walkthrough](#walkthrough) section. The guidance in this section is helpful for updating an app to authenticate and authorize users. However, an alternative approach to updating an app is to create a new app from the guidance in the [Walkthrough](#walkthrough) section and moving the app's components, classes, and resources to the new app.

aspnetcore/blazor/security/webassembly/standalone-with-azure-active-directory-b2c.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ Use one of the following approaches to run the app:
105105
* Press <kbd>F5</kbd>.
106106
* .NET CLI command shell: Execute the `dotnet watch` (or `dotnet run`) command from the app's folder.
107107

108+
## Remote authentication paths
109+
110+
[!INCLUDE[](~/blazor/security/includes/remote-authentication-paths.md)]
111+
108112
## Parts of the app
109113

110114
This section describes the parts of an app generated from the Blazor WebAssembly project template and how the app is configured. There's no specific guidance to follow in this section for a basic working application if you created the app using the guidance in the [Walkthrough](#walkthrough) section. The guidance in this section is helpful for updating an app to authenticate and authorize users. However, an alternative approach to updating an app is to create a new app from the guidance in the [Walkthrough](#walkthrough) section and moving the app's components, classes, and resources to the new app.

aspnetcore/blazor/security/webassembly/standalone-with-microsoft-accounts.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ Use one of the following approaches to run the app:
7979
* Press <kbd>F5</kbd>.
8080
* .NET CLI command shell: Execute the `dotnet watch` (or `dotnet run`) command from the app's folder.
8181

82+
## Remote authentication paths
83+
84+
[!INCLUDE[](~/blazor/security/includes/remote-authentication-paths.md)]
85+
8286
## Parts of the app
8387

8488
This section describes the parts of an app generated from the Blazor WebAssembly project template and how the app is configured. There's no specific guidance to follow in this section for a basic working application if you created the app using the guidance in the [Walkthrough](#walkthrough) section. The guidance in this section is helpful for updating an app to authenticate and authorize users. However, an alternative approach to updating an app is to create a new app from the guidance in the [Walkthrough](#walkthrough) section and moving the app's components, classes, and resources to the new app.
@@ -199,6 +203,7 @@ The <xref:Microsoft.AspNetCore.Components.Authorization?displayProperty=fullName
199203

200204
[!INCLUDE[](~/blazor/security/includes/authentication-component.md)]
201205

206+
202207
## Troubleshoot
203208

204209
[!INCLUDE[](~/blazor/security/includes/troubleshoot-wasm.md)]

aspnetcore/blazor/security/webassembly/standalone-with-microsoft-entra-id.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ Use one of the following approaches to run the app:
8383
* Press <kbd>F5</kbd>.
8484
* .NET CLI command shell: Execute the `dotnet watch` (or `dotnet run`) command from the app's folder.
8585

86+
## Remote authentication paths
87+
88+
[!INCLUDE[](~/blazor/security/includes/remote-authentication-paths.md)]
89+
8690
## Parts of the app
8791

8892
This section describes the parts of an app generated from the Blazor WebAssembly project template and how the app is configured. There's no specific guidance to follow in this section for a basic working application if you created the app using the guidance in the [Walkthrough](#walkthrough) section. The guidance in this section is helpful for updating an app to authenticate and authorize users. However, an alternative approach to updating an app is to create a new app from the guidance in the [Walkthrough](#walkthrough) section and moving the app's components, classes, and resources to the new app.

0 commit comments

Comments
 (0)