Set Identity/Account/Login as Blazor Server landing page?

Viewed 2048

I have read some solutions regarding similar requests but none have worked for me and I have no idea which way to go.

I'm using ASP.Net Core 3.0, my Blazor application includes the standard authentication option ticked on creation and all I wish to do is the following:

In my "MainLayout.razor" component page I have set the following:

 <AuthorizeView>
     <Authorized>
         <!--Navbar, APP Body and Footer configuration HTML which works -->
     </Authorized>
     <NotAuthorized>
         <!--Simple button that gets displayed and on click goes to the login page-->
         <button onclick="location.href = 'Identity/Account/Login';" class="btn btn-primary">Enter</button>
     </NotAuthorized>
 </AuthorizeView>

I would like to instead of having to click that button have the process automated so that it navigates to that page when a user is not authorized automatically.

Is this possible as I have tried multiple ways of Navigation Manager and Startup configs but can't seem to just get the routing going.

The head of the "Identity/Account/Login" page I have set to " @Page "/Account/Login" ".

If I simply open my browser and just add "...com/Account/Login " to the URL end it directs to the correct page, I believe this should be something simple but I can't seem to figure it out.

Any help would be much appreciated.

Thanks.

1 Answers

I found a nice sample in the CarChecker demo:

...
<NotAuthorized>
    <Redirect Url="Identity/Account/Login" />
</NotAuthorized>

and in Redirect.razor

@inject NavigationManager Navigation
@code{
    [Parameter] public string Url { get; set; }

    protected override void OnInitialized()
    {        
        Navigation.NavigateTo(Url);
    }
}

but in Blazor serverside there is an open issue with NavigateTo() and render-mode="ServerPrerendered".

Somehow that is not resolved yet (in 3.x, don't know about 5rc1) .

Also, I notice that this only works for Login with Navigation.NavigateTo(Url, true); which makes it a less general control. Still the idea is nice and can be adapted.

Related