|
| 1 | +--- |
| 2 | +title: Razor Pages authorization conventions in ASP.NET Core |
| 3 | +author: wadepickett |
| 4 | +description: Learn how to control access to pages with conventions that authorize users and allow anonymous users to access pages or folders of pages. |
| 5 | +monikerRange: '>= aspnetcore-2.1' |
| 6 | +ms.author: wpickett |
| 7 | +ms.custom: mvc |
| 8 | +ms.date: 03/25/2026 |
| 9 | +uid: razor-pages/security/authorization/conventions |
| 10 | +--- |
| 11 | +# Razor Pages authorization conventions in ASP.NET Core |
| 12 | + |
| 13 | +One way to control access in a Razor Pages app is to use authorization conventions at startup. These conventions allow the app to authorize users and allow anonymous users to access individual pages or folders of pages. The conventions described in this article automatically apply [authorization filters](xref:mvc/controllers/filters#authorization-filters) to control access. |
| 14 | + |
| 15 | +[View or download sample code](https://github.com/dotnet/AspNetCore.Docs.Samples/tree/main/security/authorization/RazorPagesAuthorization) ([how to download](xref:fundamentals/index#how-to-download-a-sample)) |
| 16 | + |
| 17 | +The sample app uses [cookie authentication without ASP.NET Core Identity](xref:security/authentication/cookie). To use ASP.NET Core Identity, follow the guidance in <xref:security/authentication/identity>. |
| 18 | + |
| 19 | +## Require authorization to access a page |
| 20 | + |
| 21 | +Use the <xref:Microsoft.Extensions.DependencyInjection.PageConventionCollectionExtensions.AuthorizePage%2A> convention to add an <xref:Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter> to the page at the specified path: |
| 22 | + |
| 23 | +:::moniker range=">= aspnetcore-10.0" |
| 24 | + |
| 25 | +:::code language="csharp" source="~/../AspNetCore.Docs.Samples/security/authorization/RazorPagesAuthorization/10.x/AuthorizationSample/Program.cs" id="snippet1" highlight="3"::: |
| 26 | + |
| 27 | +:::moniker-end |
| 28 | + |
| 29 | +:::moniker range=">= aspnetcore-3.0 < aspnetcore-10.0" |
| 30 | + |
| 31 | +:::code language="csharp" source="~/../AspNetCore.Docs.Samples/security/authorization/RazorPagesAuthorization/3.x/AuthorizationSample/Startup.cs" id="snippet1" highlight="3"::: |
| 32 | + |
| 33 | +:::moniker-end |
| 34 | + |
| 35 | +:::moniker range="< aspnetcore-3.0" |
| 36 | + |
| 37 | +:::code language="csharp" source="~/../AspNetCore.Docs.Samples/security/authorization/RazorPagesAuthorization/2.x/AuthorizationSample/Startup.cs" id="snippet1" highlight="2,4"::: |
| 38 | + |
| 39 | +:::moniker-end |
| 40 | + |
| 41 | +The specified path is the View Engine path, which is the Razor Pages root relative path without an extension and containing only forward slashes. |
| 42 | + |
| 43 | +To specify an [authorization policy](xref:security/authorization/policies), use an [`AuthorizePage` overload](xref:Microsoft.Extensions.DependencyInjection.PageConventionCollectionExtensions.AuthorizePage%2A): |
| 44 | + |
| 45 | +```csharp |
| 46 | +options.Conventions.AuthorizePage("/Contact", "AtLeast21"); |
| 47 | +``` |
| 48 | + |
| 49 | +> [!NOTE] |
| 50 | +> An <xref:Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter> can be applied to a page model class with the `[Authorize]` filter attribute. For more information, see <xref:razor-pages/filter#authorize-filter-attribute>. |
| 51 | +
|
| 52 | +## Require authorization to access a folder of pages |
| 53 | + |
| 54 | +Use the <xref:Microsoft.Extensions.DependencyInjection.PageConventionCollectionExtensions.AuthorizeFolder%2A> convention to add an <xref:Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter> to all of the pages in a folder at the specified path: |
| 55 | + |
| 56 | +:::moniker range=">= aspnetcore-10.0" |
| 57 | + |
| 58 | +:::code language="csharp" source="~/../AspNetCore.Docs.Samples/security/authorization/RazorPagesAuthorization/10.x/AuthorizationSample/Program.cs" id="snippet1" highlight="4"::: |
| 59 | + |
| 60 | +:::moniker-end |
| 61 | + |
| 62 | +:::moniker range=">= aspnetcore-3.0 < aspnetcore-10.0" |
| 63 | + |
| 64 | +:::code language="csharp" source="~/../AspNetCore.Docs.Samples/security/authorization/RazorPagesAuthorization/3.x/AuthorizationSample/Startup.cs" id="snippet1" highlight="4"::: |
| 65 | + |
| 66 | +:::moniker-end |
| 67 | + |
| 68 | +:::moniker range="< aspnetcore-3.0" |
| 69 | + |
| 70 | +:::code language="csharp" source="~/../AspNetCore.Docs.Samples/security/authorization/RazorPagesAuthorization/2.x/AuthorizationSample/Startup.cs" id="snippet1" highlight="2,5"::: |
| 71 | + |
| 72 | +:::moniker-end |
| 73 | + |
| 74 | +The specified path is the View Engine path, which is the Razor Pages root relative path containing only forward slashes. |
| 75 | + |
| 76 | +To specify an [authorization policy](xref:security/authorization/policies), use an [`AuthorizeFolder` overload](xref:Microsoft.Extensions.DependencyInjection.PageConventionCollectionExtensions.AuthorizeFolder%2A): |
| 77 | + |
| 78 | +```csharp |
| 79 | +options.Conventions.AuthorizeFolder("/Private", "AtLeast21"); |
| 80 | +``` |
| 81 | + |
| 82 | +## Require authorization to access an area page |
| 83 | + |
| 84 | +Use the <xref:Microsoft.Extensions.DependencyInjection.PageConventionCollectionExtensions.AuthorizeAreaPage%2A> convention to add an <xref:Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter> to the area page at the specified path: |
| 85 | + |
| 86 | +```csharp |
| 87 | +options.Conventions.AuthorizeAreaPage("Identity", "/Manage/Accounts"); |
| 88 | +``` |
| 89 | + |
| 90 | +The page name is the path of the file without an extension relative to the pages root directory for the specified area. For example, the page name for the file `Areas/Identity/Pages/Manage/Accounts.cshtml` is `/Manage/Accounts`. |
| 91 | + |
| 92 | +To specify an [authorization policy](xref:security/authorization/policies), use an [`AuthorizeAreaPage` overload](xref:Microsoft.Extensions.DependencyInjection.PageConventionCollectionExtensions.AuthorizeAreaPage%2A): |
| 93 | + |
| 94 | +```csharp |
| 95 | +options.Conventions.AuthorizeAreaPage("Identity", "/Manage/Accounts", "AtLeast21"); |
| 96 | +``` |
| 97 | + |
| 98 | +## Require authorization to access a folder of areas |
| 99 | + |
| 100 | +Use the <xref:Microsoft.Extensions.DependencyInjection.PageConventionCollectionExtensions.AuthorizeAreaFolder%2A> convention to add an <xref:Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter> to all of the areas in a folder at the specified path: |
| 101 | + |
| 102 | +```csharp |
| 103 | +options.Conventions.AuthorizeAreaFolder("Identity", "/Manage"); |
| 104 | +``` |
| 105 | + |
| 106 | +The folder path is the path of the folder relative to the pages root directory for the specified area. For example, the folder path for the files under `Areas/Identity/Pages/Manage/` is `/Manage`. |
| 107 | + |
| 108 | +To specify an [authorization policy](xref:security/authorization/policies), use an [`AuthorizeAreaFolder` overload](xref:Microsoft.Extensions.DependencyInjection.PageConventionCollectionExtensions.AuthorizeAreaFolder%2A): |
| 109 | + |
| 110 | +```csharp |
| 111 | +options.Conventions.AuthorizeAreaFolder("Identity", "/Manage", "AtLeast21"); |
| 112 | +``` |
| 113 | + |
| 114 | +## Allow anonymous access to a page |
| 115 | + |
| 116 | +Use the <xref:Microsoft.Extensions.DependencyInjection.PageConventionCollectionExtensions.AllowAnonymousToPage%2A> convention to add an <xref:Microsoft.AspNetCore.Mvc.Authorization.AllowAnonymousFilter> to a page at the specified path: |
| 117 | + |
| 118 | +:::moniker range=">= aspnetcore-10.0" |
| 119 | + |
| 120 | +:::code language="csharp" source="~/../AspNetCore.Docs.Samples/security/authorization/RazorPagesAuthorization/10.x/AuthorizationSample/Program.cs" id="snippet1" highlight="5"::: |
| 121 | + |
| 122 | +:::moniker-end |
| 123 | + |
| 124 | +:::moniker range=">= aspnetcore-3.0 < aspnetcore-10.0" |
| 125 | + |
| 126 | +:::code language="csharp" source="~/../AspNetCore.Docs.Samples/security/authorization/RazorPagesAuthorization/3.x/AuthorizationSample/Startup.cs" id="snippet1" highlight="5"::: |
| 127 | + |
| 128 | +:::moniker-end |
| 129 | + |
| 130 | +:::moniker range="< aspnetcore-3.0" |
| 131 | + |
| 132 | +:::code language="csharp" source="~/../AspNetCore.Docs.Samples/security/authorization/RazorPagesAuthorization/2.x/AuthorizationSample/Startup.cs" id="snippet1" highlight="2,6"::: |
| 133 | + |
| 134 | +:::moniker-end |
| 135 | + |
| 136 | +The specified path is the View Engine path, which is the Razor Pages root relative path without an extension and containing only forward slashes. |
| 137 | + |
| 138 | +## Allow anonymous access to a folder of pages |
| 139 | + |
| 140 | +Use the <xref:Microsoft.Extensions.DependencyInjection.PageConventionCollectionExtensions.AllowAnonymousToFolder%2A> convention to add an <xref:Microsoft.AspNetCore.Mvc.Authorization.AllowAnonymousFilter> to all of the pages in a folder at the specified path: |
| 141 | + |
| 142 | +:::moniker range=">= aspnetcore-10.0" |
| 143 | + |
| 144 | +:::code language="csharp" source="~/../AspNetCore.Docs.Samples/security/authorization/RazorPagesAuthorization/10.x/AuthorizationSample/Program.cs" id="snippet1" highlight="6"::: |
| 145 | + |
| 146 | +:::moniker-end |
| 147 | + |
| 148 | +:::moniker range=">= aspnetcore-3.0 < aspnetcore-10.0" |
| 149 | + |
| 150 | +:::code language="csharp" source="~/../AspNetCore.Docs.Samples/security/authorization/RazorPagesAuthorization/3.x/AuthorizationSample/Startup.cs" id="snippet1" highlight="6"::: |
| 151 | + |
| 152 | +:::moniker-end |
| 153 | + |
| 154 | +:::moniker range="< aspnetcore-3.0" |
| 155 | + |
| 156 | +:::code language="csharp" source="~/../AspNetCore.Docs.Samples/security/authorization/RazorPagesAuthorization/2.x/AuthorizationSample/Startup.cs" id="snippet1" highlight="2,7"::: |
| 157 | + |
| 158 | +:::moniker-end |
| 159 | + |
| 160 | +The specified path is the View Engine path, which is the Razor Pages root relative path without an extension and containing only forward slashes. |
| 161 | + |
| 162 | +## Note on combining authorized and anonymous access |
| 163 | + |
| 164 | +The app can specify that a folder of pages requires authorization and that a page within that folder allows anonymous access: |
| 165 | + |
| 166 | +```csharp |
| 167 | +// This works. |
| 168 | +.AuthorizeFolder("/Private").AllowAnonymousToPage("/Private/Public") |
| 169 | +``` |
| 170 | + |
| 171 | +The reverse, however, isn't valid. The app can't declare a folder of pages for anonymous access and specify a page within that folder that requires authorization: |
| 172 | + |
| 173 | +```csharp |
| 174 | +// This doesn't work! |
| 175 | +.AllowAnonymousToFolder("/Public").AuthorizePage("/Public/Private") |
| 176 | +``` |
| 177 | + |
| 178 | +Requiring authorization on the Private page fails. When both the <xref:Microsoft.AspNetCore.Mvc.Authorization.AllowAnonymousFilter> and <xref:Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter> are applied to the page, the <xref:Microsoft.AspNetCore.Mvc.Authorization.AllowAnonymousFilter> takes precedence and controls access. |
| 179 | + |
| 180 | +## Additional resources |
| 181 | + |
| 182 | +* <xref:razor-pages/razor-pages-conventions> |
| 183 | +* <xref:Microsoft.AspNetCore.Mvc.ApplicationModels.PageConventionCollection> |
0 commit comments