From c9099558025f2738c191814a5360a1e7e334ae62 Mon Sep 17 00:00:00 2001 From: Stephen Halter Date: Mon, 27 Nov 2023 19:32:05 -0800 Subject: [PATCH] Improve Blazor project template (#52234) # Use consistent code style in Blazor project templates - Removed usage of top-level statements in client project if "Do not use top-level statements" is selected - Removed extra "@" in `@render-mode` values - Always use `typeof(Namespace._Imports).Assembly` instead of `typeof(Counter).Assembly` so the compilation does not break when the sample Counter component is removed, and so the code is more consistent with how it is when no sample content is generated - Added Account/AccessDenied endpoint to individual auth option to match Identity UI razor pages. This is shown when the user is authenticated but unauthorized by default. Fixes #52079 Fixes #52084 Fixes #52167 ## Customer Impact In addition to not using top-level statements when the customer requests that we don't, this improves code style consistency within the Blazor project template and with the Blazor docs. ## Regression? - [ ] Yes - [x] No ## Risk - [ ] High - [ ] Medium - [x] Low These are small stylistic changes to the Blazor project templates. ## Verification - [x] Manual (required) - [ ] Automated ## Packaging changes reviewed? - [ ] Yes - [ ] No - [x] N/A --- .../.template.config/template.json | 6 +++-- .../BlazorWeb-CSharp.Client/Program.Main.cs | 23 +++++++++++++++++++ .../Account/Pages/AccessDenied.razor | 8 +++++++ .../BlazorWeb-CSharp/Components/App.razor | 16 ++++++------- .../BlazorWeb-CSharp/Components/Routes.razor | 10 ++++---- .../BlazorWeb-CSharp/Program.Main.cs | 4 +--- .../BlazorWeb-CSharp/Program.cs | 4 +--- .../Templates.Tests/template-baselines.json | 9 ++++++++ 8 files changed, 59 insertions(+), 21 deletions(-) create mode 100644 src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWeb-CSharp.Client/Program.Main.cs create mode 100644 src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWeb-CSharp/Components/Account/Pages/AccessDenied.razor diff --git a/src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/.template.config/template.json b/src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/.template.config/template.json index 9a5a22be993f..14332c669958 100644 --- a/src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/.template.config/template.json +++ b/src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/.template.config/template.json @@ -73,13 +73,15 @@ { "condition": "(!UseProgramMain)", "exclude": [ - "BlazorWeb-CSharp/Program.Main.cs" + "BlazorWeb-CSharp/Program.Main.cs", + "BlazorWeb-CSharp.Client/Program.Main.cs" ] }, { "condition": "(UseProgramMain)", "exclude": [ - "BlazorWeb-CSharp/Program.cs" + "BlazorWeb-CSharp/Program.cs", + "BlazorWeb-CSharp.Client/Program.cs" ], "rename": { "Program.Main.cs": "Program.cs" diff --git a/src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWeb-CSharp.Client/Program.Main.cs b/src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWeb-CSharp.Client/Program.Main.cs new file mode 100644 index 000000000000..1e5ff5788ec3 --- /dev/null +++ b/src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWeb-CSharp.Client/Program.Main.cs @@ -0,0 +1,23 @@ +#if (IndividualLocalAuth) +using BlazorWeb_CSharp.Client; +using Microsoft.AspNetCore.Components.Authorization; +#endif +using Microsoft.AspNetCore.Components.WebAssembly.Hosting; + +namespace BlazorWeb_CSharp.Client; + +class Program +{ + static async Task Main(string[] args) + { + var builder = WebAssemblyHostBuilder.CreateDefault(args); + + #if (IndividualLocalAuth) + builder.Services.AddAuthorizationCore(); + builder.Services.AddCascadingAuthenticationState(); + builder.Services.AddSingleton(); + + #endif + await builder.Build().RunAsync(); + } +} diff --git a/src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWeb-CSharp/Components/Account/Pages/AccessDenied.razor b/src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWeb-CSharp/Components/Account/Pages/AccessDenied.razor new file mode 100644 index 000000000000..905dec34875a --- /dev/null +++ b/src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWeb-CSharp/Components/Account/Pages/AccessDenied.razor @@ -0,0 +1,8 @@ +@page "/Account/AccessDenied" + +Access denied + +
+

Access denied

+

You do not have access to this resource.

+
diff --git a/src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWeb-CSharp/Components/App.razor b/src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWeb-CSharp/Components/App.razor index bbcd88d8b74c..113db756040d 100644 --- a/src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWeb-CSharp/Components/App.razor +++ b/src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWeb-CSharp/Components/App.razor @@ -16,13 +16,13 @@ @*#if (!InteractiveAtRoot) ##elseif (IndividualLocalAuth) - + ##elseif (UseServer && UseWebAssembly) - + ##elseif (UseServer) - + ##else - + ##endif*@ @@ -30,13 +30,13 @@ @*#if (!InteractiveAtRoot) ##elseif (IndividualLocalAuth) - + ##elseif (UseServer && UseWebAssembly) - + ##elseif (UseServer) - + ##else - + ##endif*@ diff --git a/src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWeb-CSharp/Components/Routes.razor b/src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWeb-CSharp/Components/Routes.razor index ee69ff447329..fdf2fe8b427f 100644 --- a/src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWeb-CSharp/Components/Routes.razor +++ b/src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWeb-CSharp/Components/Routes.razor @@ -2,20 +2,20 @@ @using BlazorWeb_CSharp.Components.Account.Shared ##endif*@ @*#if (UseWebAssembly && !InteractiveAtRoot) - + ##else - + ##endif*@ @*#if (IndividualLocalAuth) - + ##else - + ##endif*@ - + diff --git a/src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWeb-CSharp/Program.Main.cs b/src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWeb-CSharp/Program.Main.cs index bf9df79e72fc..9ce0e2c541b9 100644 --- a/src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWeb-CSharp/Program.Main.cs +++ b/src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWeb-CSharp/Program.Main.cs @@ -124,9 +124,7 @@ public static void Main(string[] args) #else app.MapRazorComponents(); #endif - #if (UseWebAssembly && SampleContent) - .AddAdditionalAssemblies(typeof(Counter).Assembly); - #elif (UseWebAssembly) + #if (UseWebAssembly) .AddAdditionalAssemblies(typeof(Client._Imports).Assembly); #endif diff --git a/src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWeb-CSharp/Program.cs b/src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWeb-CSharp/Program.cs index 49118765d29c..d65c123d1f21 100644 --- a/src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWeb-CSharp/Program.cs +++ b/src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWeb-CSharp/Program.cs @@ -118,9 +118,7 @@ #else app.MapRazorComponents(); #endif -#if (UseWebAssembly && SampleContent) - .AddAdditionalAssemblies(typeof(Counter).Assembly); -#elif (UseWebAssembly) +#if (UseWebAssembly) .AddAdditionalAssemblies(typeof(BlazorWeb_CSharp.Client._Imports).Assembly); #endif diff --git a/src/ProjectTemplates/test/Templates.Tests/template-baselines.json b/src/ProjectTemplates/test/Templates.Tests/template-baselines.json index 7904cc203e19..b61d22bd88dc 100644 --- a/src/ProjectTemplates/test/Templates.Tests/template-baselines.json +++ b/src/ProjectTemplates/test/Templates.Tests/template-baselines.json @@ -540,6 +540,7 @@ "Components/Account/IdentityNoOpEmailSender.cs", "Components/Account/IdentityRedirectManager.cs", "Components/Account/IdentityUserAccessor.cs", + "Components/Account/Pages/AccessDenied.razor", "Components/Account/Pages/ConfirmEmail.razor", "Components/Account/Pages/ConfirmEmailChange.razor", "Components/Account/Pages/ExternalLogin.razor", @@ -642,6 +643,7 @@ "Components/Account/IdentityRedirectManager.cs", "Components/Account/IdentityRevalidatingAuthenticationStateProvider.cs", "Components/Account/IdentityUserAccessor.cs", + "Components/Account/Pages/AccessDenied.razor", "Components/Account/Pages/ConfirmEmail.razor", "Components/Account/Pages/ConfirmEmailChange.razor", "Components/Account/Pages/ExternalLogin.razor", @@ -718,6 +720,7 @@ "Components/Account/IdentityRedirectManager.cs", "Components/Account/IdentityRevalidatingAuthenticationStateProvider.cs", "Components/Account/IdentityUserAccessor.cs", + "Components/Account/Pages/AccessDenied.razor", "Components/Account/Pages/ConfirmEmail.razor", "Components/Account/Pages/ConfirmEmailChange.razor", "Components/Account/Pages/ExternalLogin.razor", @@ -836,6 +839,7 @@ "{ProjectName}/Components/Account/IdentityNoOpEmailSender.cs", "{ProjectName}/Components/Account/IdentityRedirectManager.cs", "{ProjectName}/Components/Account/IdentityUserAccessor.cs", + "{ProjectName}/Components/Account/Pages/AccessDenied.razor", "{ProjectName}/Components/Account/Pages/ConfirmEmail.razor", "{ProjectName}/Components/Account/Pages/ConfirmEmailChange.razor", "{ProjectName}/Components/Account/Pages/ExternalLogin.razor", @@ -953,6 +957,7 @@ "{ProjectName}/Components/Account/IdentityNoOpEmailSender.cs", "{ProjectName}/Components/Account/IdentityRedirectManager.cs", "{ProjectName}/Components/Account/IdentityUserAccessor.cs", + "{ProjectName}/Components/Account/Pages/AccessDenied.razor", "{ProjectName}/Components/Account/Pages/ConfirmEmail.razor", "{ProjectName}/Components/Account/Pages/ConfirmEmailChange.razor", "{ProjectName}/Components/Account/Pages/ExternalLogin.razor", @@ -1214,6 +1219,7 @@ "{ProjectName}/Components/Account/IdentityNoOpEmailSender.cs", "{ProjectName}/Components/Account/IdentityRedirectManager.cs", "{ProjectName}/Components/Account/IdentityUserAccessor.cs", + "{ProjectName}/Components/Account/Pages/AccessDenied.razor", "{ProjectName}/Components/Account/Pages/ConfirmEmail.razor", "{ProjectName}/Components/Account/Pages/ConfirmEmailChange.razor", "{ProjectName}/Components/Account/Pages/ExternalLogin.razor", @@ -1282,6 +1288,7 @@ "Components/Account/IdentityRedirectManager.cs", "Components/Account/IdentityRevalidatingAuthenticationStateProvider.cs", "Components/Account/IdentityUserAccessor.cs", + "Components/Account/Pages/AccessDenied.razor", "Components/Account/Pages/ConfirmEmail.razor", "Components/Account/Pages/ConfirmEmailChange.razor", "Components/Account/Pages/ExternalLogin.razor", @@ -1375,6 +1382,7 @@ "{ProjectName}/Components/Account/IdentityNoOpEmailSender.cs", "{ProjectName}/Components/Account/IdentityRedirectManager.cs", "{ProjectName}/Components/Account/IdentityUserAccessor.cs", + "{ProjectName}/Components/Account/Pages/AccessDenied.razor", "{ProjectName}/Components/Account/Pages/ConfirmEmail.razor", "{ProjectName}/Components/Account/Pages/ConfirmEmailChange.razor", "{ProjectName}/Components/Account/Pages/ExternalLogin.razor", @@ -1459,6 +1467,7 @@ "{ProjectName}/Components/Account/IdentityNoOpEmailSender.cs", "{ProjectName}/Components/Account/IdentityRedirectManager.cs", "{ProjectName}/Components/Account/IdentityUserAccessor.cs", + "{ProjectName}/Components/Account/Pages/AccessDenied.razor", "{ProjectName}/Components/Account/Pages/ConfirmEmail.razor", "{ProjectName}/Components/Account/Pages/ConfirmEmailChange.razor", "{ProjectName}/Components/Account/Pages/ExternalLogin.razor",