Home > database >  get back to login page again after click on forgot password in Blazor web assembly app
get back to login page again after click on forgot password in Blazor web assembly app

Time:08-18

I have a "Blazer Web assembly" app with a login page(Component) and a Forgot Password page(Component).

When I click on the "Forgot Password" link on the Login page, instead of sending me to the "forgot password" page, it sends me back again to the login page.

Here is my code:

App.Razor

<CascadingAuthenticationState>
<Router AppAssembly="@typeof(App).Assembly">
    <Found Context="routeData">
            <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
              
            <NotAuthorized>                  
            </NotAuthorized>
        </AuthorizeRouteView>
    </Found>
    <NotFound>    
            <LayoutView Layout="@typeof(MainLayout)">              
            </LayoutView>     
    </NotFound>
</Router>
</CascadingAuthenticationState>

MainLayout.razor:

    @inherits LayoutComponentBase
     <AuthorizeView>
        <Authorized>        
    <NavBar>
        <NavBarLeft>....

@code {

    [CascadingParameter]
    Task<AuthenticationState> AuthenticationState { get; set; }

    protected override async Task OnParametersSetAsync()
    { 
        navBarLeftInjectableMenu.SetContent(null, false);      
        if (!(await AuthenticationState).User.Identity.IsAuthenticated)
        {
            NavigationManager.NavigateTo("/login");
        }
    }

Forgot Password Page:

@page "/ForgotPass"
@layout AuthLayout

<div class=....

Login Page:

@layout AuthLayout
@page "/LoginX"

@inject NavigationManager NavigationManager


<div >
    <div >   
        <button @onclick="ForgotPassword" >Forgot Password</button>
    </div>  
</div>



    @code {

        void ForgotPassword()
        {
            NavigationManager.NavigateTo("/ForgotPassX", true);

        }   
    }

AuthLayout.razor:

@inherits LayoutComponentBase

<div >
    <div >
        @Body
    </div>
</div>

HttpInterceptor:

private async Task InterceptBeforeSendAsync(object sender,  HttpClientInterceptorEventArgs e)
    {   
        
        var absolutePath = e.Request.RequestUri != null? e.Request.RequestUri.AbsolutePath : string.Empty;

        if (!absolutePath.Contains("token") && !absolutePath.Contains("acc"))
        {
            var token = await _refreshTokenService.TryRefreshToken();
            if (!string.IsNullOrEmpty(token))
            {
                e.Request.Headers.Authorization =
                    new AuthenticationHeaderValue("bearer", token);
            }
        }
    }

CodePudding user response:

You have 2 options:

1/ Put @attribute [AllowAnonymous] in your forgot password page.

2/ Create a different Layout with no Authorize required then use that for your forgot password page like @layout AnonymousLayout

CodePudding user response:

This is because you call the OnParametersSetAsync method every time you request, and your judgment condition is forcibly directed to the Login page.

You can add an extra judgment to bypass the IsAuthenticated check.

For example: Use the request url to determine whether to execute this check:

protected override async Task OnParametersSetAsync()
{ 
    string currentUrl = NavigationManager.Uri;
    if (!currentUrl.Contains("ForgotPass")){
        navBarLeftInjectableMenu.SetContent(null, false);      
        if ((!(await AuthenticationState).User.Identity.IsAuthenticated))
        {
             NavigationManager.NavigateTo("/Login");
        }
    }
}
  • Related