Skip to content

Commit 772e1c5

Browse files
authored
Add RedirectToLogin configured login path (#35988)
1 parent 1f6987d commit 772e1c5

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

aspnetcore/blazor/security/includes/redirecttologin-component.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,29 @@ The `RedirectToLogin` component (`RedirectToLogin.razor`):
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.
99

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+
1012
[!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+

0 commit comments

Comments
 (0)