Allow Anonymous user on a Blazor page

Meisam Dehghan 46 Reputation points
2021-01-28T09:18:46.403+00:00

I've developed a blazor application.I'd like to exclude one of the pages from the authentication requirement so that anonymous users can visit it.I added the following line to the top of the page,but it didn't take effect,redirecting the user to the login page!

@attribute [AllowAnonymous]

Here is the MainLayout code:

@inherits LayoutComponentBase
<div class="sidebar">
    <NavMenu />
</div>
<div class="main">
    <div class="content px-4">
        <AuthorizeView>
            <Authorized>
                @Body
            </Authorized>
            <NotAuthorized>
                <DaniarAppAPI.Pages.Login />
            </NotAuthorized>
        </AuthorizeView>
    </div>
</div>
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,479 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiadong Meng - MSFT 126 Reputation points
    2021-01-29T07:37:21.203+00:00

    Hi @Meisam Dehghan ,

    The [AllowAnonymous] won't work here, since <AuthorizeView> in layout page has distinguished between authorized users and unauthorized users, it even doesn't reach [AllowAnonymous] in specify page.

    So, if you want to exclude some pages from the authentication requirement, one approach is to have a same layout without AuthorizeView, and make those page inherit this layout.


    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,

    Jiadong Meng

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful