From ffc52783cadc36f690a3b11790ed5c05c02cf086 Mon Sep 17 00:00:00 2001 From: Luke Latham <1622880+guardrex@users.noreply.github.com> Date: Sun, 3 Mar 2019 17:01:46 -0600 Subject: [PATCH] Razor Pages authz conventions updates (#11246) Fixes #6301 [Internal Review Topic](https://review.docs.microsoft.com/en-us/aspnet/core/security/authorization/razor-pages-authorization?view=aspnetcore-2.1&branch=pr-en-us-11246) * Sample to 2.2 * Lean out sample (remove *wwwroot* and references to static assets) * Drop the cookie policy bits (but leave the HTTPS given that this is an authz samp) * Add examples of method policy overloads + a link to the policies topic * Topic UE pass * XREF links --- .../razor-pages-authorization.md | 57 +- .../AuthorizationSample.csproj | 3 +- .../Pages/Account/Login.cshtml | 42 +- .../Pages/Account/Login.cshtml.cs | 8 +- .../AuthorizationSample/Pages/Error.cshtml | 11 +- .../AuthorizationSample/Pages/Error.cshtml.cs | 8 +- .../AuthorizationSample/Pages/Index.cshtml | 113 +- .../Pages/Shared/_CookieConsentPartial.cshtml | 41 - .../Pages/Shared/_Layout.cshtml | 78 +- .../Shared/_ValidationScriptsPartial.cshtml | 18 - .../Pages/_LoginPartial.cshtml | 8 +- .../samples/2.x/AuthorizationSample/README.md | 4 +- .../2.x/AuthorizationSample/Startup.cs | 9 +- .../appsettings.Production.json | 9 - .../AuthorizationSample/wwwroot/css/site.css | 48 - .../AuthorizationSample/wwwroot/favicon.ico | Bin 32038 -> 0 bytes .../wwwroot/images/banner1.svg | 1 - .../wwwroot/images/banner2.svg | 1 - .../wwwroot/images/banner3.svg | 1 - .../wwwroot/images/banner4.svg | 1 - .../AuthorizationSample/wwwroot/js/site.js | 1 - .../wwwroot/lib/bootstrap/.bower.json | 45 - .../wwwroot/lib/bootstrap/LICENSE | 21 - .../bootstrap/dist/css/bootstrap-theme.css | 587 - .../lib/bootstrap/dist/css/bootstrap.css | 6757 ---------- .../fonts/glyphicons-halflings-regular.eot | Bin 20127 -> 0 bytes .../fonts/glyphicons-halflings-regular.svg | 288 - .../fonts/glyphicons-halflings-regular.ttf | Bin 45404 -> 0 bytes .../fonts/glyphicons-halflings-regular.woff | Bin 23424 -> 0 bytes .../fonts/glyphicons-halflings-regular.woff2 | Bin 18028 -> 0 bytes .../lib/bootstrap/dist/js/bootstrap.js | 2377 ---- .../wwwroot/lib/bootstrap/dist/js/npm.js | 13 - .../jquery-validation-unobtrusive/.bower.json | 15 - .../jquery-validation-unobtrusive/LICENSE.txt | 12 - .../jquery.validate.unobtrusive.js | 431 - .../wwwroot/lib/jquery-validation/.bower.json | 41 - .../wwwroot/lib/jquery-validation/LICENSE.md | 22 - .../dist/additional-methods.js | 1158 -- .../jquery-validation/dist/jquery.validate.js | 1601 --- .../wwwroot/lib/jquery/.bower.json | 26 - .../wwwroot/lib/jquery/LICENSE.txt | 36 - .../wwwroot/lib/jquery/dist/jquery.js | 10364 ---------------- 42 files changed, 96 insertions(+), 24160 deletions(-) delete mode 100644 aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/Pages/Shared/_CookieConsentPartial.cshtml delete mode 100644 aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/Pages/Shared/_ValidationScriptsPartial.cshtml delete mode 100644 aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/appsettings.Production.json delete mode 100644 aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/wwwroot/css/site.css delete mode 100644 aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/wwwroot/favicon.ico delete mode 100644 aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/wwwroot/images/banner1.svg delete mode 100644 aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/wwwroot/images/banner2.svg delete mode 100644 aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/wwwroot/images/banner3.svg delete mode 100644 aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/wwwroot/images/banner4.svg delete mode 100644 aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/wwwroot/js/site.js delete mode 100644 aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/wwwroot/lib/bootstrap/.bower.json delete mode 100644 aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/wwwroot/lib/bootstrap/LICENSE delete mode 100644 aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/wwwroot/lib/bootstrap/dist/css/bootstrap-theme.css delete mode 100644 aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/wwwroot/lib/bootstrap/dist/css/bootstrap.css delete mode 100644 aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/wwwroot/lib/bootstrap/dist/fonts/glyphicons-halflings-regular.eot delete mode 100644 aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/wwwroot/lib/bootstrap/dist/fonts/glyphicons-halflings-regular.svg delete mode 100644 aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/wwwroot/lib/bootstrap/dist/fonts/glyphicons-halflings-regular.ttf delete mode 100644 aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/wwwroot/lib/bootstrap/dist/fonts/glyphicons-halflings-regular.woff delete mode 100644 aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/wwwroot/lib/bootstrap/dist/fonts/glyphicons-halflings-regular.woff2 delete mode 100644 aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/wwwroot/lib/bootstrap/dist/js/bootstrap.js delete mode 100644 aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/wwwroot/lib/bootstrap/dist/js/npm.js delete mode 100644 aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/wwwroot/lib/jquery-validation-unobtrusive/.bower.json delete mode 100644 aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/wwwroot/lib/jquery-validation-unobtrusive/LICENSE.txt delete mode 100644 aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/wwwroot/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js delete mode 100644 aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/wwwroot/lib/jquery-validation/.bower.json delete mode 100644 aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/wwwroot/lib/jquery-validation/LICENSE.md delete mode 100644 aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/wwwroot/lib/jquery-validation/dist/additional-methods.js delete mode 100644 aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/wwwroot/lib/jquery-validation/dist/jquery.validate.js delete mode 100644 aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/wwwroot/lib/jquery/.bower.json delete mode 100644 aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/wwwroot/lib/jquery/LICENSE.txt delete mode 100644 aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/wwwroot/lib/jquery/dist/jquery.js diff --git a/aspnetcore/security/authorization/razor-pages-authorization.md b/aspnetcore/security/authorization/razor-pages-authorization.md index 0145e9c585ef..e78ace423326 100644 --- a/aspnetcore/security/authorization/razor-pages-authorization.md +++ b/aspnetcore/security/authorization/razor-pages-authorization.md @@ -2,9 +2,10 @@ title: Razor Pages authorization conventions in ASP.NET Core author: guardrex description: Learn how to control access to pages with conventions that authorize users and allow anonymous users to access pages or folders of pages. +monikerRange: '>= aspnetcore-2.1' ms.author: riande ms.custom: mvc -ms.date: 10/27/2017 +ms.date: 03/03/2019 uid: security/authorization/razor-pages-authorization --- # Razor Pages authorization conventions in ASP.NET Core @@ -15,40 +16,42 @@ One way to control access in your Razor Pages app is to use authorization conven [View or download sample code](https://github.com/aspnet/Docs/tree/master/aspnetcore/security/authorization/razor-pages-authorization/samples) ([how to download](xref:index#how-to-download-a-sample)) -The sample app uses [Cookie authentication without ASP.NET Core Identity](xref:security/authentication/cookie). The user account for the hypothetical user, Maria Rodriguez, is hardcoded into the app. Use the Email username "maria.rodriguez@contoso.com" and any password to sign in the user. The user is authenticated in the `AuthenticateUser` method in the *Pages/Account/Login.cshtml.cs* file. In a real-world example, the user would be authenticated against a database. To use ASP.NET Core Identity, follow the guidance in the [Introduction to Identity on ASP.NET Core](xref:security/authentication/identity) topic. The concepts and examples shown in this topic apply equally to apps that use ASP.NET Core Identity. +The sample app uses [cookie authentication without ASP.NET Core Identity](xref:security/authentication/cookie). The concepts and examples shown in this topic apply equally to apps that use ASP.NET Core Identity. To use ASP.NET Core Identity, follow the guidance in . ## Require authorization to access a page -Use the [AuthorizePage](/dotnet/api/microsoft.extensions.dependencyinjection.pageconventioncollectionextensions.authorizepage) convention via [AddRazorPagesOptions](/dotnet/api/microsoft.extensions.dependencyinjection.mvcrazorpagesmvcbuilderextensions.addrazorpagesoptions) to add an [AuthorizeFilter](/dotnet/api/microsoft.aspnetcore.mvc.authorization.authorizefilter) to the page at the specified path: +Use the convention via to add an to the page at the specified path: [!code-csharp[](razor-pages-authorization/samples/2.x/AuthorizationSample/Startup.cs?name=snippet1&highlight=2,4)] The specified path is the View Engine path, which is the Razor Pages root relative path without an extension and containing only forward slashes. -An [AuthorizePage overload](/dotnet/api/microsoft.extensions.dependencyinjection.pageconventioncollectionextensions.authorizepage#Microsoft_Extensions_DependencyInjection_PageConventionCollectionExtensions_AuthorizePage_Microsoft_AspNetCore_Mvc_ApplicationModels_PageConventionCollection_System_String_System_String_) is available if you need to specify an authorization policy. +To specify an [authorization policy](xref:security/authorization/policies), use an [AuthorizePage overload](xref:Microsoft.Extensions.DependencyInjection.PageConventionCollectionExtensions.AuthorizePage*): -::: moniker range=">= aspnetcore-2.1" +```csharp +options.Conventions.AuthorizePage("/Contact", "AtLeast21"); +``` > [!NOTE] -> An `AuthorizeFilter` can be applied to a page model class with the `[Authorize]` filter attribute. For more information, see [Authorize filter attribute](xref:razor-pages/filter#authorize-filter-attribute). - -::: moniker-end +> An can be applied to a page model class with the `[Authorize]` filter attribute. For more information, see [Authorize filter attribute](xref:razor-pages/filter#authorize-filter-attribute). ## Require authorization to access a folder of pages -Use the [AuthorizeFolder](/dotnet/api/microsoft.extensions.dependencyinjection.pageconventioncollectionextensions.authorizefolder) convention via [AddRazorPagesOptions](/dotnet/api/microsoft.extensions.dependencyinjection.mvcrazorpagesmvcbuilderextensions.addrazorpagesoptions) to add an [AuthorizeFilter](/dotnet/api/microsoft.aspnetcore.mvc.authorization.authorizefilter) to all of the pages in a folder at the specified path: +Use the convention via to add an to all of the pages in a folder at the specified path: [!code-csharp[](razor-pages-authorization/samples/2.x/AuthorizationSample/Startup.cs?name=snippet1&highlight=2,5)] The specified path is the View Engine path, which is the Razor Pages root relative path. -An [AuthorizeFolder overload](/dotnet/api/microsoft.extensions.dependencyinjection.pageconventioncollectionextensions.authorizefolder#Microsoft_Extensions_DependencyInjection_PageConventionCollectionExtensions_AuthorizeFolder_Microsoft_AspNetCore_Mvc_ApplicationModels_PageConventionCollection_System_String_System_String_) is available if you need to specify an authorization policy. +To specify an [authorization policy](xref:security/authorization/policies), use an [AuthorizeFolder overload](xref:Microsoft.Extensions.DependencyInjection.PageConventionCollectionExtensions.AuthorizeFolder*): -::: moniker range=">= aspnetcore-2.1" +```csharp +options.Conventions.AuthorizeFolder("/Private", "AtLeast21"); +``` ## Require authorization to access an area page -Use the [AuthorizeAreaPage](/dotnet/api/microsoft.extensions.dependencyinjection.pageconventioncollectionextensions.authorizeareapage) convention via [AddRazorPagesOptions](/dotnet/api/microsoft.extensions.dependencyinjection.mvcrazorpagesmvcbuilderextensions.addrazorpagesoptions) to add an [AuthorizeFilter](/dotnet/api/microsoft.aspnetcore.mvc.authorization.authorizefilter) to the area page at the specified path: +Use the convention via to add an to the area page at the specified path: ```csharp options.Conventions.AuthorizeAreaPage("Identity", "/Manage/Accounts"); @@ -56,11 +59,15 @@ options.Conventions.AuthorizeAreaPage("Identity", "/Manage/Accounts"); 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*. -An [AuthorizeAreaPage overload](/dotnet/api/microsoft.extensions.dependencyinjection.pageconventioncollectionextensions.authorizeareapage#Microsoft_Extensions_DependencyInjection_PageConventionCollectionExtensions_AuthorizeAreaPage_Microsoft_AspNetCore_Mvc_ApplicationModels_PageConventionCollection_System_String_System_String_System_String_) is available if you need to specify an authorization policy. +To specify an [authorization policy](xref:security/authorization/policies), use an [AuthorizeAreaPage overload](xref:Microsoft.Extensions.DependencyInjection.PageConventionCollectionExtensions.AuthorizeAreaPage*): + +```csharp +options.Conventions.AuthorizeAreaPage("Identity", "/Manage/Accounts", "AtLeast21"); +``` ## Require authorization to access a folder of areas -Use the [AuthorizeAreaFolder](/dotnet/api/microsoft.extensions.dependencyinjection.pageconventioncollectionextensions.authorizeareafolder) convention via [AddRazorPagesOptions](/dotnet/api/microsoft.extensions.dependencyinjection.mvcrazorpagesmvcbuilderextensions.addrazorpagesoptions) to add an [AuthorizeFilter](/dotnet/api/microsoft.aspnetcore.mvc.authorization.authorizefilter) to all of the areas in a folder at the specified path: +Use the convention via to add an to all of the areas in a folder at the specified path: ```csharp options.Conventions.AuthorizeAreaFolder("Identity", "/Manage"); @@ -68,13 +75,15 @@ options.Conventions.AuthorizeAreaFolder("Identity", "/Manage"); 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*. -An [AuthorizeAreaFolder overload](/dotnet/api/microsoft.extensions.dependencyinjection.pageconventioncollectionextensions.authorizeareafolder#Microsoft_Extensions_DependencyInjection_PageConventionCollectionExtensions_AuthorizeAreaFolder_Microsoft_AspNetCore_Mvc_ApplicationModels_PageConventionCollection_System_String_System_String_System_String_) is available if you need to specify an authorization policy. +To specify an [authorization policy](xref:security/authorization/policies), use an [AuthorizeAreaFolder overload](xref:Microsoft.Extensions.DependencyInjection.PageConventionCollectionExtensions.AuthorizeAreaFolder*): -::: moniker-end +```csharp +options.Conventions.AuthorizeAreaFolder("Identity", "/Manage", "AtLeast21"); +``` ## Allow anonymous access to a page -Use the [AllowAnonymousToPage](/dotnet/api/microsoft.extensions.dependencyinjection.pageconventioncollectionextensions.allowanonymoustopage) convention via [AddRazorPagesOptions](/dotnet/api/microsoft.extensions.dependencyinjection.mvcrazorpagesmvcbuilderextensions.addrazorpagesoptions) to add an [AllowAnonymousFilter](/dotnet/api/microsoft.aspnetcore.mvc.authorization.allowanonymousfilter) to a page at the specified path: +Use the convention via to add an to a page at the specified path: [!code-csharp[](razor-pages-authorization/samples/2.x/AuthorizationSample/Startup.cs?name=snippet1&highlight=2,6)] @@ -82,7 +91,7 @@ The specified path is the View Engine path, which is the Razor Pages root relati ## Allow anonymous access to a folder of pages -Use the [AllowAnonymousToFolder](/dotnet/api/microsoft.extensions.dependencyinjection.pageconventioncollectionextensions.allowanonymoustofolder) convention via [AddRazorPagesOptions](/dotnet/api/microsoft.extensions.dependencyinjection.mvcrazorpagesmvcbuilderextensions.addrazorpagesoptions) to add an [AllowAnonymousFilter](/dotnet/api/microsoft.aspnetcore.mvc.authorization.allowanonymousfilter) to all of the pages in a folder at the specified path: +Use the convention via to add an to all of the pages in a folder at the specified path: [!code-csharp[](razor-pages-authorization/samples/2.x/AuthorizationSample/Startup.cs?name=snippet1&highlight=2,7)] @@ -90,23 +99,23 @@ The specified path is the View Engine path, which is the Razor Pages root relati ## Note on combining authorized and anonymous access -It's perfectly valid to specify that a folder of pages require authorization and specify that a page within that folder allows anonymous access: +It's valid to specify that a folder of pages that require authorization and than specify that a page within that folder allows anonymous access: ```csharp // This works. .AuthorizeFolder("/Private").AllowAnonymousToPage("/Private/Public") ``` -The reverse, however, isn't true. You can't declare a folder of pages for anonymous access and specify a page within for authorization: +The reverse, however, isn't valid. You can't declare a folder of pages for anonymous access and then specify a page within that folder that requires authorization: ```csharp // This doesn't work! -.AllowAnonymousToFolder("/Public").AuthorizePage("/Public/Private") +.AllowAnonymousToFolder("/Public").AuthorizePage("/Public/Private") ``` -Requiring authorization on the Private page won't work because when both the `AllowAnonymousFilter` and `AuthorizeFilter` filters are applied to the page, the `AllowAnonymousFilter` wins and controls access. +Requiring authorization on the Private page fails. When both the and are applied to the page, the takes precedence and controls access. ## Additional resources -* [Razor Pages custom route and page model providers](xref:razor-pages/razor-pages-conventions) -* [PageConventionCollection](/dotnet/api/microsoft.aspnetcore.mvc.applicationmodels.pageconventioncollection) class +* +* diff --git a/aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/AuthorizationSample.csproj b/aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/AuthorizationSample.csproj index 46d7a54addf4..d1202da20b2b 100644 --- a/aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/AuthorizationSample.csproj +++ b/aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/AuthorizationSample.csproj @@ -1,7 +1,8 @@ - netcoreapp2.1 + netcoreapp2.2 + InProcess diff --git a/aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/Pages/Account/Login.cshtml b/aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/Pages/Account/Login.cshtml index c1a22f57392d..0b7b901a98d2 100644 --- a/aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/Pages/Account/Login.cshtml +++ b/aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/Pages/Account/Login.cshtml @@ -6,28 +6,22 @@

@ViewData["Title"]

-
-
-

Use a local account to log in.

-
-
-
- - - -
-
- - - -
-
- -
-
-
+
+

Use a local account to log in.

+
+
+
+ + + +
+
+ + + +
+
+ +
+
- -@section Scripts { - @await Html.PartialAsync("_ValidationScriptsPartial") -} diff --git a/aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/Pages/Account/Login.cshtml.cs b/aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/Pages/Account/Login.cshtml.cs index 37210a47522d..f8e1223429c7 100644 --- a/aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/Pages/Account/Login.cshtml.cs +++ b/aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/Pages/Account/Login.cshtml.cs @@ -1,14 +1,14 @@ +using System; +using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Security.Claims; using System.Threading.Tasks; using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using AuthorizationSample.Data; -using Microsoft.AspNetCore.Authentication.Cookies; -using System.Collections.Generic; - namespace AuthorizationSample.Pages.Account { public class LoginModel : PageModel @@ -87,7 +87,7 @@ private async Task AuthenticateUser(string email, string passwo await Task.Delay(500); - if (email == "maria.rodriguez@contoso.com") + if (string.Equals(email, "maria.rodriguez@contoso.com", StringComparison.OrdinalIgnoreCase)) { return new ApplicationUser() { diff --git a/aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/Pages/Error.cshtml b/aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/Pages/Error.cshtml index e9369fe65a9a..693d5fde8b45 100644 --- a/aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/Pages/Error.cshtml +++ b/aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/Pages/Error.cshtml @@ -4,8 +4,8 @@ ViewData["Title"] = "Error"; } -

Error.

-

An error occurred while processing your request.

+

Error.

+

An error occurred while processing your request.

@if (Model.ShowRequestId) { @@ -16,8 +16,11 @@

Development Mode

- Swapping to Development environment will display more detailed information about the error that occurred. + Swapping to the Development environment displays detailed information about the error that occurred.

- Development environment should not be enabled in deployed applications, as it can result in sensitive information from exceptions being displayed to end users. For local debugging, development environment can be enabled by setting the ASPNETCORE_ENVIRONMENT environment variable to Development and restarting the app or adding .UseEnvironment("Development") to WebHost in Program.cs and restarting the app. + The Development environment shouldn't be enabled for deployed applications. + It can result in displaying sensitive information from exceptions to end users. + For local debugging, enable the Development environment by setting the ASPNETCORE_ENVIRONMENT environment variable to Development + and restarting the app.

diff --git a/aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/Pages/Error.cshtml.cs b/aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/Pages/Error.cshtml.cs index 7781e2f0e621..cb1f50cb4fb6 100644 --- a/aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/Pages/Error.cshtml.cs +++ b/aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/Pages/Error.cshtml.cs @@ -1,16 +1,20 @@ +using System; +using System.Collections.Generic; using System.Diagnostics; +using System.Linq; +using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; namespace AuthorizationSample.Pages { + [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public class ErrorModel : PageModel { public string RequestId { get; set; } - + public bool ShowRequestId => !string.IsNullOrEmpty(RequestId); - [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public void OnGet() { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier; diff --git a/aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/Pages/Index.cshtml b/aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/Pages/Index.cshtml index 182244def4a2..e2ab8a97fb67 100644 --- a/aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/Pages/Index.cshtml +++ b/aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/Pages/Index.cshtml @@ -1,120 +1,13 @@ @page @model IndexModel @{ - ViewData["Title"] = "Home page"; + ViewData["Title"] = "Authorization Sample"; } @if (User.Identity.IsAuthenticated) {
-
-
Hello @User.Claims.FirstOrDefault(c => c.Type == "FullName")?.Value!
-

Username: @User.Identity.Name

-
+
Hello @User.Claims.FirstOrDefault(c => c.Type == "FullName")?.Value!
+

Username: @User.Identity.Name

} - - - - diff --git a/aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/Pages/Shared/_CookieConsentPartial.cshtml b/aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/Pages/Shared/_CookieConsentPartial.cshtml deleted file mode 100644 index 315cfe0ee489..000000000000 --- a/aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/Pages/Shared/_CookieConsentPartial.cshtml +++ /dev/null @@ -1,41 +0,0 @@ -@using Microsoft.AspNetCore.Http.Features - -@{ - var consentFeature = Context.Features.Get(); - var showBanner = !consentFeature?.CanTrack ?? false; - var cookieString = consentFeature?.CreateConsentCookie(); -} - -@if (showBanner) -{ - - -} diff --git a/aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/Pages/Shared/_Layout.cshtml b/aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/Pages/Shared/_Layout.cshtml index a8610580f03c..8c4cdd8f53b4 100644 --- a/aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/Pages/Shared/_Layout.cshtml +++ b/aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/Pages/Shared/_Layout.cshtml @@ -3,76 +3,30 @@ - @ViewData["Title"] - Authorization Sample - - - - - - - - + @ViewData["Title"] + - - - + +
  • Contact (Authentication Required)
  • + + @await Html.PartialAsync("_LoginPartial") +
    @RenderBody() -
    -
    -

    ©@System.DateTime.Now.Year - Authorization Sample

    -
    - - - - - - - - - - - @RenderSection("Scripts", required: false) diff --git a/aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/Pages/Shared/_ValidationScriptsPartial.cshtml b/aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/Pages/Shared/_ValidationScriptsPartial.cshtml deleted file mode 100644 index ef848fe8fc8c..000000000000 --- a/aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/Pages/Shared/_ValidationScriptsPartial.cshtml +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - - - diff --git a/aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/Pages/_LoginPartial.cshtml b/aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/Pages/_LoginPartial.cshtml index 4b66fed4a5fc..d5faf3da2438 100644 --- a/aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/Pages/_LoginPartial.cshtml +++ b/aspnetcore/security/authorization/razor-pages-authorization/samples/2.x/AuthorizationSample/Pages/_LoginPartial.cshtml @@ -1,16 +1,16 @@ @if (User.Identity.IsAuthenticated) { -