Blazor Server - Make Specific Pages Anonymous by Keeping Root Folder Authorized

Mathews, Jaish 21 Reputation points
2021-06-17T19:42:46.797+00:00

0

I am using .NET5 Blazor server application. I have below code in my Startup.cs, which is making sure that always show OKTA login page if user not authenticated.

services.AddRazorPages(options =>
            {
                options.Conventions.AuthorizeFolder("/");
                options.Conventions.AllowAnonymousToPage("/anonymous");

                //No error here but no effect either
            }
            );

If you notice that I want to access a page "/anonymous", wthout authentication. But always it's routing to OKTA. How can I skip this routing and directly access "/anonymous"? I must need to keep the line options.Conventions.AuthorizeFolder("/");

My App.razor has bene enabled with CascadingAuthenticationState view, which also need to keep.

<CascadingAuthenticationState>
    <Router AppAssembly="@typeof(Program).Assembly">
        <Found Context="routeData">
            <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
                <NotAuthorized>
                    <h4>Sorry, you're not authorized to reach this page.</h4>
                </NotAuthorized>
                <Authorizing>
                    <h4>Authentication in progress...</h4>
                </Authorizing>
            </AuthorizeRouteView>
        </Found>
        <NotFound>
            <LayoutView Layout="@typeof(MainLayout)">
                <p>Sorry, there's nothing at this address.</p>
            </LayoutView>
        </NotFound>
    </Router>
</CascadingAuthenticationState>

i tried appied @attribute [AllowAnonymous] as well. Any lead would be appreciated

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,403 questions
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,500 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yinqiu Yao-MSFT 236 Reputation points
    2021-06-18T01:31:05.783+00:00

    Hi @Mathews, Jaish

    It seems that the .razor files aren't part of those authorization options.

    Unless you create a cshtml file, you can use options.Conventions.AllowAnonymousToPage

    Best regards
    Yinqiu Yao


    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.


0 additional answers

Sort by: Most helpful