Search code examples
azureasp.net-coreazure-active-directoryrazor-pages

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" />


Solution

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

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