Exception shown while redirecting to the login page in Blazor Server
Mohammad Nasir Uddin
41
Reputation points
I have created a Blazor Server project in .NET 6. I want to implement my own custom authentication in this project. So I have created a login component under Page folder. And I have configured my App.razor file below so that while not authenticated it redirects to the login page.
<CascadingAuthenticationState>
<Router AppAssembly="@typeof(App).Assembly">
<Found Context="routeData">
<AuthorizeView>
<Authorized>
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
</Authorized>
<NotAuthorized>
<LoginRedirect></LoginRedirect>
</NotAuthorized>
</AuthorizeView>
</Found>
<NotFound>
<PageTitle>Not found</PageTitle>
<AuthorizeView>
<Authorized>
<LayoutView Layout="@typeof(MainLayout)">
<p role="alert">Sorry, there's nothing at this address.</p>
</LayoutView>
</Authorized>
<NotAuthorized>
<LoginRedirect></LoginRedirect>
</NotAuthorized>
</AuthorizeView>
</NotFound>
</Router>
</CascadingAuthenticationState>
Here is my LoginRedirect component code:
using Microsoft.AspNetCore.Components;
namespace BlazorJwtToken.Shared
{
public partial class LoginRedirect
{
[Inject] NavigationManager? NavigationManager { get; set; }
protected override Task OnInitializedAsync()
{
var returnUrl = "/" + NavigationManager?.ToBaseRelativePath(NavigationManager.Uri);
NavigationManager?.NavigateTo($"/login?returnUrl={returnUrl}", false);
return Task.CompletedTask;
}
}
}
But when I run the project it hits the OnInitializedAsync function of LoginRedirect component and throws the below exception:
Can anyone tell me how to solve the problem?