Blazor wasm skip CascadingAuthenticationState for index page.

Somnath Shukla 411 Reputation points
2021-08-01T16:17:47.86+00:00

I am using blazer wasm and hosting it asp.net core default template. it has both secure and unsecure page.
by default when I will start the app. it show loading and then authorization. since my home/index page is not secure. i want to skip this authorization check for this page. for faster loading.

<CascadingAuthenticationState>
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
<NotAuthorized>
@if (!context.User.Identity.IsAuthenticated)
{
<RedirectToLogin />
}
else
{
<p role="alert">You are not authorized to access this resource.</p>
}
</NotAuthorized>
</AuthorizeRouteView>
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
</Found>
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
<p role="alert">Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
</CascadingAuthenticationState>

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,382 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yiyi You - MSFT 521 Reputation points
    2021-08-02T02:32:12.953+00:00

    Hi,@Somnath Shukla ,

    You can try to get current url with NavigationManager,and check the url,if the url is not index page url,the page needs authorization.

    @inject NavigationManager NavigationManager  
        <CascadingAuthenticationState>  
        <Router AppAssembly="@typeof(Program).Assembly">  
        <Found Context="routeData">  
        @if (NavigationManager.Uri != NavigationManager.BaseUri)  
        {  
        <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">  
        <NotAuthorized>  
        @if (!context.User.Identity.IsAuthenticated)  
        {  
        <RedirectToLogin />  
        }  
        else  
        {  
        <p role="alert">You are not authorized to access this resource.</p>  
        }  
        </NotAuthorized>  
        </AuthorizeRouteView>  
        }  
        else  
        {  
         <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />  
        }  
        <FocusOnNavigate RouteData="@routeData" Selector="h1" />  
        </Found>  
        <NotFound>  
        <LayoutView Layout="@typeof(MainLayout)">  
        <p role="alert">Sorry, there's nothing at this address.</p>  
        </LayoutView>  
        </NotFound>  
        </Router>  
        </CascadingAuthenticationState>  
    

    ----------

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards,
    YiyiYou


0 additional answers

Sort by: Most helpful