Jio
Jio

Reputation: 107

.NET Core Razor pages change pages to anonymous

I have a project with .NET Core 2.1 Razor Pages. It was built using the authentication option in the new project UI. So, when I type the URL and hit enter, the website opens the Microsoft login page, and I cannot see any page until I am signed in.

Now, I have a request to change the behavior of the authentication and I need to show the home page (Index) and other pages without the authentication process. So, I think I have to move the login page to a button and allow those pages to be accessible by anyone.

Can you please show me the right direction for this change?

I see that the [AllowAnonymous] directive is only for the controller (ASP.NET Core MVC), because it is not working in the Razor page, where the client and server code are combined in 2 files.

Also, I found a post talking about Service.AddRazorPage but I got an error showing that this method is for MVC, so I don't know if I have to continue with my research to work with this method.

Thanks!

Upvotes: 2

Views: 825

Answers (1)

Guru Stron
Guru Stron

Reputation: 142833

You can try using Razor Pages authorization conventions in ASP.NET Core:

services.AddMvc()
    .AddRazorPagesOptions(options =>
    {
        // options.Conventions.AuthorizePage("/Contact");
        // options.Conventions.AuthorizeFolder("/Private");
        options.Conventions.AllowAnonymousToPage("/Index");
        // options.Conventions.AllowAnonymousToFolder("/Private/PublicPages");
    })
    // ...
    ;

Upvotes: 2

Related Questions