Too many redirects problem error while redirecting to the login page in Blazor server

Mohammad Nasir Uddin 41 Reputation points
2022-08-28T19:27:48.9+00:00

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/Account folder. In that account folder there is a new Layout page. 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:

public partial class LoginRedirect  
{  
    [Inject] NavigationManager? NavigationManager { get; set; }  
      
    protected override async Task OnInitializedAsync()  
    {  
        var returnUrl = NavigationManager?.ToBaseRelativePath(NavigationManager.Uri);  
          
        NavigationManager?.NavigateTo($"/account/login/{returnUrl}", false);  
  
        await base.OnInitializedAsync();  
    }  
}  

When I am running the project I get the below error:

235611-image.png

I have uploaded the project in github. Here is the github link . Can anyone help me to solve the problem?

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,400 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Brando Zhang-MSFT 2,961 Reputation points Microsoft Vendor
    2022-08-29T09:15:09.307+00:00

    Hi @Mohammad Nasir Uddin ,

    The reason why you don't execute the redirect well is you put it inside the OnInitializedAsync method , I suggest you could try below codes and you will find it work well.

     public partial class LoginRedirect  
    {  
        [Inject] NavigationManager? NavigationManager { get; set; }  
          
        protected override async Task OnInitializedAsync()  
        {  
             
      
            await base.OnInitializedAsync();  
        }  
      
        protected override void OnAfterRender(bool firstRender)  
        {  
            var returnUrl = NavigationManager?.ToBaseRelativePath(NavigationManager.Uri);  
      
            NavigationManager?.NavigateTo($"/account/login/{returnUrl}", false);  
      
            base.OnAfterRender(firstRender);  
        }  
    }  
    
    
      
    
    0 comments No comments