From 93cae2c8b272da2f2161daa2b4eb5e4fb3d12779 Mon Sep 17 00:00:00 2001 From: Luke Latham <1622880+guardrex@users.noreply.github.com> Date: Mon, 13 Apr 2020 19:25:25 -0500 Subject: [PATCH 1/8] Scaffolding into Blazor Server apps Updates Updates Updates Update aspnetcore/includes/scaffold-identity/id-scaffold-dlg-auth.md Co-Authored-By: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Remove dup Identity code in Startup Use the relative path for Login redirect Login redirect requires the force flag Surface a Identity pages styling approach --- aspnetcore/includes/scaffold-identity/hsts.md | 2 +- .../scaffold-identity/id-scaffold-dlg-auth.md | 34 ++- .../scaffold-identity/id-scaffold-dlg.md | 17 +- .../authentication/scaffold-identity.md | 245 ++++++++++++++++-- .../3.1sample/StartupBlazor.cs | 36 +++ .../scaffold-identity/3.1sample/StartupMVC.cs | 5 - .../scaffold-identity/3.1sample/StartupRP.cs | 6 - aspnetcore/security/blazor/index.md | 58 +++-- aspnetcore/security/blazor/server.md | 108 ++++++-- 9 files changed, 404 insertions(+), 107 deletions(-) create mode 100644 aspnetcore/security/authentication/scaffold-identity/3.1sample/StartupBlazor.cs diff --git a/aspnetcore/includes/scaffold-identity/hsts.md b/aspnetcore/includes/scaffold-identity/hsts.md index 50963294d274..359c0000fbbb 100644 --- a/aspnetcore/includes/scaffold-identity/hsts.md +++ b/aspnetcore/includes/scaffold-identity/hsts.md @@ -1 +1 @@ -`UseHsts` is recommended but not required. See [HTTP Strict Transport Security Protocol](xref:security/enforcing-ssl#http-strict-transport-security-protocol-hsts) for more information. +`UseHsts` is recommended but not required. For more information, see [HTTP Strict Transport Security Protocol](xref:security/enforcing-ssl#http-strict-transport-security-protocol-hsts). diff --git a/aspnetcore/includes/scaffold-identity/id-scaffold-dlg-auth.md b/aspnetcore/includes/scaffold-identity/id-scaffold-dlg-auth.md index 22038acb216f..71a57d6ff018 100644 --- a/aspnetcore/includes/scaffold-identity/id-scaffold-dlg-auth.md +++ b/aspnetcore/includes/scaffold-identity/id-scaffold-dlg-auth.md @@ -7,16 +7,14 @@ Run the Identity scaffolder: * From **Solution Explorer**, right-click on the project > **Add** > **New Scaffolded Item**. * From the left pane of the **Add Scaffold** dialog, select **Identity** > **Add**. * In the **Add Identity** dialog, select the options you want. - * Select your existing layout page, or your layout file will be overwritten with incorrect markup. When an existing *\_Layout.cshtml* file is selected, it is **not** overwritten. - - For example: - `~/Pages/Shared/_Layout.cshtml` for Razor Pages - `~/Views/Shared/_Layout.cshtml` for MVC projects + * Select your existing layout page so your layout file isn't overwritten with incorrect markup. When an existing *\_Layout.cshtml* file is selected, it is **not** overwritten. For example: + * `~/Pages/Shared/_Layout.cshtml` for Razor Pages or Blazor Server projects with existing Razor Pages infrastructure + * `~/Views/Shared/_Layout.cshtml` for MVC projects or Blazor Server projects with existing MVC infrastructure * To use your existing data context, select at least one file to override. You must select at least one file to add your data context. * Select your data context class. * Select **Add**. * To create a new user context and possibly create a custom user class for Identity: - * Select the **+** button to create a new **Data context class**. + * Select the **+** button to create a new **Data context class**. Accept the default value or specify a class (for example, `MyApplication.Data.ApplicationDbContext`). * Select **Add**. Note: If you're creating a new user context, you don't have to select a file to override. @@ -29,7 +27,7 @@ If you have not previously installed the ASP.NET Core scaffolder, install it now dotnet tool install -g dotnet-aspnet-codegenerator ``` -Add required NuGet package references to the project (\*.csproj) file. Run the following command in the project directory: +Add required NuGet package references to the project file (*.csproj*). Run the following commands in the project directory: ```dotnetcli dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design @@ -51,13 +49,13 @@ dotnet aspnet-codegenerator identity -h In the project folder, run the Identity scaffolder with the options you want. For example, to setup identity with the default UI and the minimum number of files, run the following command. Use the correct fully qualified name for your DB context: ```dotnetcli -dotnet aspnet-codegenerator identity -dc MyWeb.Data.ApplicationDbContext --files "Account.Register;Account.Login" +dotnet aspnet-codegenerator identity -dc MyApplication.Data.ApplicationDbContext --files "Account.Register;Account.Login" ``` PowerShell uses semicolon as a command separator. When using PowerShell, escape the semi-colons in the file list or put the file list in double quotes. For example: ```dotnetcli -dotnet aspnet-codegenerator identity -dc MyWeb.Data.ApplicationDbContext --files "Account.Register;Account.Login;Account.Logout" +dotnet aspnet-codegenerator identity -dc MyApplication.Data.ApplicationDbContext --files "Account.Register;Account.Login;Account.Logout" ``` If you run the Identity scaffolder without specifying the `--files` flag or the `--useDefaultUI` flag, all the available Identity UI pages will be created in your project. @@ -75,16 +73,14 @@ Run the Identity scaffolder: * From **Solution Explorer**, right-click on the project > **Add** > **New Scaffolded Item**. * From the left pane of the **Add Scaffold** dialog, select **Identity** > **Add**. * In the **Add Identity** dialog, select the options you want. - * Select your existing layout page, or your layout file will be overwritten with incorrect markup. When an existing *\_Layout.cshtml* file is selected, it is **not** overwritten. - - For example: - `~/Pages/Shared/_Layout.cshtml` for Razor Pages - `~/Views/Shared/_Layout.cshtml` for MVC projects + * Select your existing layout page, or your layout file will be overwritten with incorrect markup. When an existing *\_Layout.cshtml* file is selected, it is **not** overwritten. For example: + * `~/Pages/Shared/_Layout.cshtml` for Razor Pages + * `~/Views/Shared/_Layout.cshtml` for MVC projects * To use your existing data context, select at least one file to override. You must select at least one file to add your data context. * Select your data context class. * Select **Add**. * To create a new user context and possibly create a custom user class for Identity: - * Select the **+** button to create a new **Data context class**. + * Select the **+** button to create a new **Data context class**. Accept the default value or specify a class (for example, `MyApplication.Data.ApplicationDbContext`). * Select **Add**. Note: If you're creating a new user context, you don't have to select a file to override. @@ -97,7 +93,7 @@ If you have not previously installed the ASP.NET Core scaffolder, install it now dotnet tool install -g dotnet-aspnet-codegenerator ``` -Add a package reference to [Microsoft.VisualStudio.Web.CodeGeneration.Design](https://www.nuget.org/packages/Microsoft.VisualStudio.Web.CodeGeneration.Design/) to the project (\*.csproj) file. Run the following command in the project directory: +Add a package reference to [Microsoft.VisualStudio.Web.CodeGeneration.Design](https://www.nuget.org/packages/Microsoft.VisualStudio.Web.CodeGeneration.Design/) to the project file (*.csproj*). Run the following commands in the project directory: ```dotnetcli dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design @@ -113,17 +109,17 @@ dotnet aspnet-codegenerator identity -h In the project folder, run the Identity scaffolder with the options you want. For example, to setup identity with the default UI and the minimum number of files, run the following command. Use the correct fully qualified name for your DB context: ```dotnetcli -dotnet aspnet-codegenerator identity -dc MyWeb.Data.ApplicationDbContext --files "Account.Register;Account.Login" +dotnet aspnet-codegenerator identity -dc MyApplication.Data.ApplicationDbContext --files "Account.Register;Account.Login" ``` PowerShell uses semicolon as a command separator. When using PowerShell, escape the semi-colons in the file list or put the file list in double quotes. For example: ```dotnetcli -dotnet aspnet-codegenerator identity -dc MyWeb.Data.ApplicationDbContext --files "Account.Register;Account.Login;Account.Logout" +dotnet aspnet-codegenerator identity -dc MyApplication.Data.ApplicationDbContext --files "Account.Register;Account.Login;Account.Logout" ``` If you run the Identity scaffolder without specifying the `--files` flag or the `--useDefaultUI` flag, all the available Identity UI pages will be created in your project. --- -::: moniker-end \ No newline at end of file +::: moniker-end diff --git a/aspnetcore/includes/scaffold-identity/id-scaffold-dlg.md b/aspnetcore/includes/scaffold-identity/id-scaffold-dlg.md index 012eee95e0b9..d8de890ca753 100644 --- a/aspnetcore/includes/scaffold-identity/id-scaffold-dlg.md +++ b/aspnetcore/includes/scaffold-identity/id-scaffold-dlg.md @@ -3,13 +3,14 @@ Run the Identity scaffolder: # [Visual Studio](#tab/visual-studio) * From **Solution Explorer**, right-click on the project > **Add** > **New Scaffolded Item**. -* From the left pane of the **Add Scaffold** dialog, select **Identity** > **ADD**. -* In the **ADD Identity** dialog, select the options you want. - * Select your existing layout page, or your layout file will be overwritten with incorrect markup. For example - `~/Pages/Shared/_Layout.cshtml` for Razor Pages - `~/Views/Shared/_Layout.cshtml` for MVC projects - * Select the **+** button to create a new **Data context class**. -* Select **ADD**. +* From the left pane of the **Add New Scaffolded Item** dialog, select **Identity** > **Add**. +* In the **Add Identity** dialog, select the options you want. + * Select your existing layout page, or your layout file will be overwritten with incorrect markup: + * `~/Pages/Shared/_Layout.cshtml` for Razor Pages + * `~/Views/Shared/_Layout.cshtml` for MVC projects + * Blazor Server apps created from the Blazor Server template (`blazorserver`) aren't configured for Razor Pages or MVC by default. Leave the layout page entry blank. + * Select the **+** button to create a new **Data context class**. Accept the default value or specify a class (for example, `MyApplication.Data.ApplicationDbContext`). +* Select **Add**. # [.NET Core CLI](#tab/netcore-cli) @@ -19,7 +20,7 @@ If you have not previously installed the ASP.NET Core scaffolder, install it now dotnet tool install -g dotnet-aspnet-codegenerator ``` -Add required NuGet package references to the project (\*.csproj) file. Run the following command in the project directory: +Add required NuGet package references to the project file (*.csproj*). Run the following commands in the project directory: ```dotnetcli dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design diff --git a/aspnetcore/security/authentication/scaffold-identity.md b/aspnetcore/security/authentication/scaffold-identity.md index 9df19e607356..7b85c594fe5a 100644 --- a/aspnetcore/security/authentication/scaffold-identity.md +++ b/aspnetcore/security/authentication/scaffold-identity.md @@ -5,7 +5,7 @@ description: Learn how to scaffold Identity in an ASP.NET Core project. monikerRange: '>= aspnetcore-2.1' ms.author: riande ms.custom: mvc -ms.date: 01/15/2020 +ms.date: 04/21/2020 uid: security/authentication/scaffold-identity --- # Scaffold Identity in ASP.NET Core projects @@ -14,7 +14,7 @@ By [Rick Anderson](https://twitter.com/RickAndMSFT) ::: moniker range=">= aspnetcore-3.0" -ASP.NET Core provides [ASP.NET Core Identity](xref:security/authentication/identity) as a [Razor Class Library](xref:razor-pages/ui-class). Applications that include Identity can apply the scaffolder to selectively add the source code contained in the Identity Razor Class Library (RCL). You might want to generate source code so you can modify the code and change the behavior. For example, you could instruct the scaffolder to generate the code used in registration. Generated code takes precedence over the same code in the Identity RCL. To gain full control of the UI and not use the default RCL, see the section [Create full identity UI source](#full). +ASP.NET Core provides [ASP.NET Core Identity](xref:security/authentication/identity) as a [Razor Class Library](xref:razor-pages/ui-class). Applications that include Identity can apply the scaffolder to selectively add the source code contained in the Identity Razor Class Library (RCL). You might want to generate source code so you can modify the code and change the behavior. For example, you could instruct the scaffolder to generate the code used in registration. Generated code takes precedence over the same code in the Identity RCL. To gain full control of the UI and not use the default RCL, see the section [Create full Identity UI source](#full). Applications that do **not** include authentication can apply the scaffolder to add the RCL Identity package. You have the option of selecting Identity code to be generated. @@ -26,7 +26,7 @@ Services are required when using [Two Factor Authentication](xref:security/authe This document contains more complete instructions than the *ScaffoldingReadme.txt* file which is generated when running the scaffolder. -## Scaffold identity into an empty project +## Scaffold Identity into an empty project [!INCLUDE[](~/includes/scaffold-identity/id-scaffold-dlg.md)] @@ -38,7 +38,7 @@ Update the `Startup` class with code similar to the following: [!INCLUDE[](~/includes/scaffold-identity/migrations.md)] -## Scaffold identity into a Razor project without existing authorization +## Scaffold Identity into a Razor project without existing authorization [!INCLUDE[](~/includes/scaffold-identity/id-scaffold-dlg-auth.md)] + Some Identity options are configured in *Areas/Identity/IdentityHostingStartup.cs*. For more information, see [IHostingStartup](xref:fundamentals/configuration/platform-specific-configuration). -## Scaffold identity into an MVC project without existing authorization +## Scaffold Identity into an MVC project without existing authorization [!INCLUDE[](~/includes/scaffold-identity/id-scaffold-dlg-auth.md)] + Some Identity options are configured in *Areas/Identity/IdentityHostingStartup.cs*. For more information, see [IHostingStartup](xref:fundamentals/configuration/platform-specific-configuration). -## Scaffold identity into an MVC project without existing authorization +## Scaffold Identity into an MVC project without existing authorization