Praveen Kumar
Praveen Kumar

Reputation: 145

How to add Azure Active Directory authentication only to some Razor Pages in a web app?

I have implemented the Azure Active Directory authentication following Azure AD Authentication on the Stackoverflow. In my project, I need the authentication only on the Settings page and the other pages to be available without any authentication. Can anyone guide me on how to achieve this? I have a dedicated layout page for the Settings page which calls the <partial name="_LoginPartial" />

Upvotes: 1

Views: 626

Answers (2)

Praveen Kumar
Praveen Kumar

Reputation: 145

Found a working sample on GitHub Similar to Mike Brind's sample, here is the approach to exclude/include folders.

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

Upvotes: 1

Mike Brind
Mike Brind

Reputation: 30045

You can configure which pages to protect from unauthorised users as follows:

services.AddRazorPages()
    .AddRazorPagesOptions(options =>
    {
        options.Conventions.AuthorizePage("/Settings");
    });

Upvotes: 0

Related Questions