Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auth with global interactivity #50920

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
e011406
Begin making it work
SteveSandersonMS Sep 25, 2023
6c49a7a
Simplify
SteveSandersonMS Sep 25, 2023
380c05b
Fix other rendermodes
SteveSandersonMS Sep 25, 2023
a369d51
Seems that nested "if" is not supported
SteveSandersonMS Sep 25, 2023
1574e2b
Start moving account files into a single subdir
SteveSandersonMS Sep 25, 2023
cb06df0
Fix paths
SteveSandersonMS Sep 25, 2023
98a222f
Add RedirectToLogin
SteveSandersonMS Sep 25, 2023
f35f3ba
Fixes for RedirectToLogin
SteveSandersonMS Sep 25, 2023
8fec9e9
Fix for per-page interactivity
SteveSandersonMS Sep 25, 2023
9136aae
Add missing BOMs
SteveSandersonMS Sep 26, 2023
a79a907
Baseline: "new blazor --interactivity none"
SteveSandersonMS Sep 26, 2023
beffe3b
Baseline NoInteractivityWithIndividualAuth
SteveSandersonMS Sep 26, 2023
92252ee
Baseline UseServerWithIndividualAuth
SteveSandersonMS Sep 26, 2023
7f77286
Baseline UseServerWithIndividualAuthAndSqlServer
SteveSandersonMS Sep 26, 2023
e7346a4
Baseline UseWebAssemblyWithIndividualAuth
SteveSandersonMS Sep 26, 2023
48621a2
Baseline UseServerAndWebAssemblyWithIndividualAuth
SteveSandersonMS Sep 26, 2023
1ec264a
Baseline EmptyUseServerAndWebAssemblyWithIndividualAuth
SteveSandersonMS Sep 26, 2023
d1164ee
Baseline ServerInteractiveAtRootWithIndividualAuth
SteveSandersonMS Sep 26, 2023
7fd02fe
More new baselines
SteveSandersonMS Sep 26, 2023
3df5adb
Fix mistake and add docs on generating baselines
SteveSandersonMS Sep 26, 2023
27534b5
Fix formatting
SteveSandersonMS Sep 26, 2023
eaf72c0
Fix baseline for UseWebAssemblyInteractiveAtRoot
SteveSandersonMS Sep 26, 2023
17158d5
Fix baseline for UseServerAndWebAssemblyInteractiveAtRoot
SteveSandersonMS Sep 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/ProjectTemplates/README-BASELINES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generating template-baselines.json

For small project template changes, you may be able to edit the `template-baselines.json` file manually. This is a good way to ensure you have correct expectations about the effects of your changes.

For larger changes such as adding entirely new templates, it may be impractical to type out the changes to `template-baselines.json` manually. In those cases you can follow a procedure like the following.

1. Ensure you've configured the necessary environment variables:
- `set PATH=c:\git\dotnet\aspnetcore\.dotnet\;%PATH%` (update path as needed)
- `set DOTNET_ROOT=c:\git\dotnet\aspnetcore\.dotnet` (update path as needed)
2. Get to a position where you can execute the modified template(s) locally, i.e.:
- Use `dotnet pack ProjectTemplatesNoDeps.slnf` (possibly with `--no-restore --no-dependencies`) to regenerate `Microsoft.DotNet.Web.ProjectTemplates.*.nupkg`
- Run one of the `scripts/*.ps1` scripts to install your template pack and execute your chosen template. For example, run `powershell .\scripts\Run-BlazorWeb-Locally.ps1`
- Once that has run, you should see your updated template listed when you execute `dotnet new list` or `dotnet new YourTemplateName --help`. At the point you can run `dotnet new YourTemplateName -o SomePath` directly if you want. However each time you edit template sources further, you will need to run `dotnet new uninstall Microsoft.DotNet.Web.ProjectTemplates.8.0` and then go back to the start of this whole step.
- Tip: the following command combines the above steps, to go directly from editing template sources to an updated local project output: `dotnet pack ProjectTemplatesNoDeps.slnf --no-restore --no-dependencies && dotnet new uninstall Microsoft.DotNet.Web.ProjectTemplates.8.0 && rm -rf scripts\MyBlazorApp && powershell .\scripts\Run-BlazorWeb-Locally.ps1`
3. After generating a particular project's output, the following can be run in a Bash prompt (e.g., using WSL):
- `cd src/ProjectTemplates/scripts`
- `export PROJECT_NAME=MyBlazorApp` (update as necessary - note this is the name of the directly under `scripts` containing your project output)
- `find $PROJECT_NAME -type f -not -path "*/obj/*" -not -path "*/bin/*" -not -path "*/.publish/*" | sed -e "s/^$PROJECT_NAME\///" | sed -e "s/$PROJECT_NAME/{ProjectName}/g" | sed 's/.*/ "&",/' | sort -f`
- This will emit the JSON-formatted lines you can manually insert into the relevant place inside `template-baselines.json`
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@
"condition": "(UseWebAssembly && InteractiveAtRoot)",
"rename": {
"BlazorWeb-CSharp/Components/Layout/": "./BlazorWeb-CSharp.Client/Layout/",
"BlazorWeb-CSharp/Components/Pages/": "./BlazorWeb-CSharp.Client/Pages/",
"BlazorWeb-CSharp/Components/Pages/Home.razor": "./BlazorWeb-CSharp.Client/Pages/Home.razor",
"BlazorWeb-CSharp/Components/Pages/Home.razor.css": "./BlazorWeb-CSharp.Client/Pages/Home.razor.css",
"BlazorWeb-CSharp/Components/Pages/Weather.razor": "./BlazorWeb-CSharp.Client/Pages/Weather.razor",
"BlazorWeb-CSharp/Components/Pages/Weather.razor.css": "./BlazorWeb-CSharp.Client/Pages/Weather.razor.css",
"BlazorWeb-CSharp/Components/Pages/Counter.razor": "./BlazorWeb-CSharp.Client/Pages/Counter.razor",
"BlazorWeb-CSharp/Components/Pages/Counter.razor.css": "./BlazorWeb-CSharp.Client/Pages/Counter.razor.css",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We had a bug here before with Error.razor being moved into the .Client project if you were using WebAssembly or Auto. To avoid this and future bugs of the same kind, this is now moving individual pages so that newly-added things are not moved by default.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify, the projects failed to compile before this fix for Error.razor, because the .Client project can't even reference HttpContext (without at least adding a reference to M.A.Http.Abstractions).

"BlazorWeb-CSharp/Components/Routes.razor": "./BlazorWeb-CSharp.Client/Routes.razor"
}
},
Expand Down Expand Up @@ -113,12 +118,8 @@
{
"condition": "(!IndividualLocalAuth)",
"exclude": [
"BlazorWeb-CSharp/Components/Identity/**",
"BlazorWeb-CSharp/Components/Layout/ManageLayout.razor",
"BlazorWeb-CSharp/Components/Layout/ManageNavMenu.razor",
"BlazorWeb-CSharp/Components/Pages/Account/**",
"BlazorWeb-CSharp/Components/Account/**",
"BlazorWeb-CSharp/Data/**",
"BlazorWeb-CSharp/Identity/**",
"BlazorWeb-CSharp.Client/PersistentAuthenticationStateProvider.cs",
"BlazorWeb-CSharp.Client/UserInfo.cs",
"BlazorWeb-CSharp.Client/Pages/Auth.razor"
Expand All @@ -127,7 +128,7 @@
{
"condition": "(!(IndividualLocalAuth && !UseLocalDB))",
"exclude": [
"BlazorWeb-CSharp/app.db"
"BlazorWeb-CSharp/Data/app.db"
]
},
{
Expand All @@ -139,19 +140,19 @@
{
"condition": "(!(IndividualLocalAuth && UseServer && UseWebAssembly))",
"exclude": [
"BlazorWeb-CSharp/Identity/PersistingRevalidatingAuthenticationStateProvider.cs"
"BlazorWeb-CSharp/Components/Account/PersistingRevalidatingAuthenticationStateProvider.cs"
]
},
{
"condition": "(!(IndividualLocalAuth && UseServer && !UseWebAssembly))",
"exclude": [
"BlazorWeb-CSharp/Identity/IdentityRevalidatingAuthenticationStateProvider.cs"
"BlazorWeb-CSharp/Components/Account/IdentityRevalidatingAuthenticationStateProvider.cs"
]
},
{
"condition": "(!(IndividualLocalAuth && !UseServer && UseWebAssembly))",
"exclude": [
"BlazorWeb-CSharp/Identity/PersistingServerAuthenticationStateProvider.cs"
"BlazorWeb-CSharp/Components/Account/PersistingServerAuthenticationStateProvider.cs"
]
},
{
Expand Down Expand Up @@ -189,6 +190,12 @@
"exclude": [
"BlazorWeb-CSharp/Data/SqlServer/**"
]
},
{
"condition": "(IndividualLocalAuth && UseWebAssembly)",
"rename": {
"BlazorWeb-CSharp/Components/Account/Shared/RedirectToLogin.razor": "BlazorWeb-CSharp.Client/RedirectToLogin.razor"
}
}
]
}
Expand Down Expand Up @@ -349,7 +356,7 @@
"defaultValue": "InteractivePerPage",
"displayName": "_Interactivity location",
"description": "Chooses which components will have interactive rendering enabled",
"isEnabled": "(InteractivityPlatform != \"None\" && auth == \"None\")",
"isEnabled": "(InteractivityPlatform != \"None\")",
"choices": [
{
"choice": "InteractivePerPage",
Expand Down Expand Up @@ -413,7 +420,7 @@
"AllInteractive": {
"type": "parameter",
"datatype": "bool",
"isEnabled": "(InteractivityPlatform != \"None\" && auth == \"None\")",
"isEnabled": "(InteractivityPlatform != \"None\")",
"defaultValue": "false",
"displayName": "_Enable interactive rendering globally throughout the site",
"description": "Configures whether to make every page interactive by applying an interactive render mode at the top level. If false, pages will use static server rendering by default, and can be marked interactive on a per-page or per-component basis."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using BlazorWeb_CSharp.Components.Pages.Account;
using BlazorWeb_CSharp.Components.Pages.Account.Manage;
using BlazorWeb_CSharp.Components.Account.Pages;
using BlazorWeb_CSharp.Components.Account.Pages.Manage;
using BlazorWeb_CSharp.Data;
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.Primitives;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Components;

namespace BlazorWeb_CSharp.Identity;
namespace BlazorWeb_CSharp.Components.Account;

internal sealed class IdentityRedirectManager(
NavigationManager navigationManager,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Microsoft.Extensions.Options;
using BlazorWeb_CSharp.Data;

namespace BlazorWeb_CSharp.Identity;
namespace BlazorWeb_CSharp.Components.Account;

public class IdentityRevalidatingAuthenticationStateProvider : RevalidatingServerAuthenticationStateProvider
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
@using Microsoft.AspNetCore.Identity
@using Microsoft.AspNetCore.WebUtilities
@using BlazorWeb_CSharp.Data
@using BlazorWeb_CSharp.Identity
@using BlazorWeb_CSharp.Components.Account

@inject UserManager<ApplicationUser> UserManager
@inject IdentityRedirectManager RedirectManager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
@using Microsoft.AspNetCore.Identity
@using Microsoft.AspNetCore.WebUtilities
@using BlazorWeb_CSharp.Data
@using BlazorWeb_CSharp.Identity
@using BlazorWeb_CSharp.Components.Account

@inject UserManager<ApplicationUser> UserManager
@inject SignInManager<ApplicationUser> SignInManager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
@using Microsoft.AspNetCore.Identity.UI.Services
@using Microsoft.AspNetCore.WebUtilities
@using BlazorWeb_CSharp.Data
@using BlazorWeb_CSharp.Identity
@using BlazorWeb_CSharp.Components.Account

@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
@using Microsoft.AspNetCore.Identity.UI.Services
@using Microsoft.AspNetCore.WebUtilities
@using BlazorWeb_CSharp.Data
@using BlazorWeb_CSharp.Identity
@using BlazorWeb_CSharp.Components.Account

@inject NavigationManager NavigationManager
@inject IdentityRedirectManager RedirectManager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@using Microsoft.AspNetCore.Identity
@using Microsoft.AspNetCore.WebUtilities
@using BlazorWeb_CSharp.Data
@using BlazorWeb_CSharp.Identity
@using BlazorWeb_CSharp.Components.Account

@inject SignInManager<ApplicationUser> SignInManager
@inject ILogger<Login> Logger
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
@using System.ComponentModel.DataAnnotations
@using Microsoft.AspNetCore.Identity
@using BlazorWeb_CSharp.Data
@using BlazorWeb_CSharp.Identity
@using BlazorWeb_CSharp.Components.Account

@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
@using Microsoft.AspNetCore.Identity
@using Microsoft.AspNetCore.Mvc
@using BlazorWeb_CSharp.Data
@using BlazorWeb_CSharp.Identity
@using BlazorWeb_CSharp.Components.Account

@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
@using System.ComponentModel.DataAnnotations
@using Microsoft.AspNetCore.Identity
@using BlazorWeb_CSharp.Data
@using BlazorWeb_CSharp.Identity
@using BlazorWeb_CSharp.Components.Account

@inject UserManager<ApplicationUser> UserManager
@inject SignInManager<ApplicationUser> SignInManager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
@using System.ComponentModel.DataAnnotations
@using Microsoft.AspNetCore.Identity
@using BlazorWeb_CSharp.Data
@using BlazorWeb_CSharp.Identity
@using BlazorWeb_CSharp.Components.Account

@inject UserManager<ApplicationUser> UserManager
@inject SignInManager<ApplicationUser> SignInManager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

@using Microsoft.AspNetCore.Identity
@using BlazorWeb_CSharp.Data
@using BlazorWeb_CSharp.Identity
@using BlazorWeb_CSharp.Components.Account

@inject UserManager<ApplicationUser> UserManager
@inject UserAccessor UserAccessor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
@using Microsoft.AspNetCore.Identity.UI.Services
@using Microsoft.AspNetCore.WebUtilities
@using BlazorWeb_CSharp.Data
@using BlazorWeb_CSharp.Identity
@using BlazorWeb_CSharp.Components.Account

@inject UserManager<ApplicationUser> UserManager
@inject UserAccessor UserAccessor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@using System.Text.Encodings.Web
@using Microsoft.AspNetCore.Identity
@using BlazorWeb_CSharp.Data
@using BlazorWeb_CSharp.Identity
@using BlazorWeb_CSharp.Components.Account

@inject UserManager<ApplicationUser> UserManager
@inject UserAccessor UserAccessor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
@using Microsoft.AspNetCore.Identity
@using Microsoft.AspNetCore.Mvc.ViewFeatures
@using BlazorWeb_CSharp.Data
@using BlazorWeb_CSharp.Identity
@using BlazorWeb_CSharp.Components.Account

@inject UserManager<ApplicationUser> UserManager
@inject SignInManager<ApplicationUser> SignInManager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

@using Microsoft.AspNetCore.Identity
@using BlazorWeb_CSharp.Data
@using BlazorWeb_CSharp.Identity
@using BlazorWeb_CSharp.Components.Account

@inject UserManager<ApplicationUser> UserManager
@inject UserAccessor UserAccessor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
@using System.Security.Claims
@using Microsoft.AspNetCore.Identity;
@using BlazorWeb_CSharp.Data;
@using BlazorWeb_CSharp.Identity
@using BlazorWeb_CSharp.Components.Account

@inject AuthenticationStateProvider AuthenticationStateProvider
@inject UserManager<ApplicationUser> UserManager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

@using Microsoft.AspNetCore.Identity
@using BlazorWeb_CSharp.Data
@using BlazorWeb_CSharp.Identity
@using BlazorWeb_CSharp.Components.Account

@inject UserManager<ApplicationUser> UserManager
@inject SignInManager<ApplicationUser> SignInManager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
@using System.ComponentModel.DataAnnotations
@using Microsoft.AspNetCore.Identity
@using BlazorWeb_CSharp.Data
@using BlazorWeb_CSharp.Identity
@using BlazorWeb_CSharp.Components.Account

@inject UserManager<ApplicationUser> UserManager
@inject SignInManager<ApplicationUser> SignInManager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
@using Microsoft.AspNetCore.Http.Features
@using Microsoft.AspNetCore.Identity
@using BlazorWeb_CSharp.Data
@using BlazorWeb_CSharp.Identity
@using BlazorWeb_CSharp.Components.Account

@inject UserManager<ApplicationUser> UserManager
@inject SignInManager<ApplicationUser> SignInManager
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@layout ManageLayout
@attribute [Microsoft.AspNetCore.Authorization.Authorize]
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
@using Microsoft.AspNetCore.Identity.UI.Services
@using Microsoft.AspNetCore.WebUtilities
@using BlazorWeb_CSharp.Data
@using BlazorWeb_CSharp.Identity
@using BlazorWeb_CSharp.Components.Account

@inject UserManager<ApplicationUser> UserManager
@inject IUserStore<ApplicationUser> UserStore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
@using Microsoft.AspNetCore.Identity.UI.Services
@using Microsoft.AspNetCore.WebUtilities
@using BlazorWeb_CSharp.Data
@using BlazorWeb_CSharp.Identity
@using BlazorWeb_CSharp.Components.Account

@inject UserManager<ApplicationUser> UserManager
@inject IEmailSender EmailSender
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
@using Microsoft.AspNetCore.Identity.UI.Services
@using Microsoft.AspNetCore.WebUtilities
@using BlazorWeb_CSharp.Data
@using BlazorWeb_CSharp.Identity
@using BlazorWeb_CSharp.Components.Account

@inject UserManager<ApplicationUser> UserManager
@inject IEmailSender EmailSender
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@using Microsoft.AspNetCore.Identity
@using Microsoft.AspNetCore.WebUtilities
@using BlazorWeb_CSharp.Data
@using BlazorWeb_CSharp.Identity
@using BlazorWeb_CSharp.Components.Account

@inject IdentityRedirectManager RedirectManager
@inject UserManager<ApplicationUser> UserManager
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@using BlazorWeb_CSharp.Components.Account.Pages
@using BlazorWeb_CSharp.Components.Account.Shared
@layout AccountLayout
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using BlazorWeb_CSharp.Client;
using BlazorWeb_CSharp.Data;

namespace BlazorWeb_CSharp.Identity;
namespace BlazorWeb_CSharp.Components.Account;

public class PersistingRevalidatingAuthenticationStateProvider : RevalidatingServerAuthenticationStateProvider
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using Microsoft.Extensions.Options;
using BlazorWeb_CSharp.Client;

namespace BlazorWeb_CSharp.Identity;
namespace BlazorWeb_CSharp.Components.Account;

public class PersistingServerAuthenticationStateProvider : ServerAuthenticationStateProvider, IDisposable
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
@inherits LayoutComponentBase
@*#if (UseWebAssembly && InteractiveAtRoot)
@layout BlazorWeb_CSharp.Client.Layout.MainLayout
##else
@layout BlazorWeb_CSharp.Components.Layout.MainLayout
##endif*@
@inject NavigationManager Nav

@if (HttpContext is null)
{
<p>Loading...</p>
}
else
{
@Body
}

@code {
[CascadingParameter] HttpContext? HttpContext { get; set; }

protected override void OnParametersSet()
{
if (HttpContext is null)
{
// If this code runs, we're currently rendering in interactive mode, so there is no HttpContext.
// The identity pages need to set cookies, so they require an HttpContext. To achieve this we
// must transition back from interactive mode to a server-rendered page.
Nav.Refresh(forceReload: true);
}
}
}
Loading
Loading