The data is not saved within a DI Scoped Service when using Middleware after Identity login. In this example, the data is always "false" even though it has been set to "true" (confirmed in the debugger).
I'm attempting to load the user's data in PostAuthenticationMiddleware.cs. I believe this is called after login.
How can I load the user data after logging in, and retrieve it throughout the Blazor Server application?

_Host.cshtml
@page "/" @namespace Blazor_Identity_Middleware_Scoped_Poc.Pages @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers @{ Layout = "_Layout"; } Program.cs
using Blazor_Identity_Middleware_Scoped_Poc.Areas.Identity; using Blazor_Identity_Middleware_Scoped_Poc.Data; using Blazor_Identity_Middleware_Scoped_Poc.Services; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Authorization; using Microsoft.AspNetCore.Components.Web; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity.UI; using Microsoft.EntityFrameworkCore; var builder = WebApplication.CreateBuilder(args); // Add services to the container. var connectionString = builder.Configuration.GetConnectionString("DefaultConnection"); builder.Services.AddDbContext(options => options.UseSqlServer(connectionString)); builder.Services.AddDatabaseDeveloperPageExceptionFilter(); builder.Services.AddDefaultIdentity(options => options.SignIn.RequireConfirmedAccount = true) .AddEntityFrameworkStores(); builder.Services.AddRazorPages(); builder.Services.AddServerSideBlazor(); builder.Services.AddScoped(); builder.Services.AddSingleton(); builder.Services.AddScoped(); // added scoped service var app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseMigrationsEndPoint(); } else { app.UseExceptionHandler("/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.UseMiddleware(); // added middleware to fire event after login, this loads the UserData app.MapControllers(); app.MapBlazorHub(); app.MapFallbackToPage("/_Host"); app.Run(); UserData.cs
namespace Blazor_Identity_Middleware_Scoped_Poc.Services { public class UserData { public bool SomeUserData { get; set; } = false; } } PostAuthenticationMiddleware.cs
using Microsoft.AspNetCore.Identity; using Microsoft.Data.SqlClient; namespace Blazor_Identity_Middleware_Scoped_Poc.Services { public class PostAuthenticationMiddleware { private readonly RequestDelegate _next; public PostAuthenticationMiddleware(RequestDelegate next) { _next = next; } public async Task InvokeAsync(HttpContext context, UserData userData, UserManager userManager) { if (context.User.Identity.IsAuthenticated) { // User is authenticated; perform your actions here // You can retrieve the user from the UserManager using the user's name or ID var user = await userManager.GetUserAsync(context.User); if (user != null) { // Do we have any data? if (userData.SomeUserData == false) { // We'll load the user data from the database. // However... This is called 4 times. Console.WriteLine("userData.SomeUserData is false"); userData.SomeUserData = true; } else { // This is never called. Console.WriteLine("userData.SomeUserData is true"); } } } // Call the next delegate/middleware in the pipeline await _next(context); } } } Counter.razor
@page "/counter" @using Blazor_Identity_Middleware_Scoped_Poc.Services @using Microsoft.AspNetCore.Components.Authorization @using System.Security.Claims @inject AuthenticationStateProvider AuthenticationStateProvider @inject UserData UserData Counter Counter Current count: @currentCount
Click me @if (user.Identity.IsAuthenticated) {
Welcome, @user.Identity.Name!
} else {
You are not logged in.
} SomeUserData: @UserData.SomeUserData @code { private int currentCount = 0; private ClaimsPrincipal user; protected override async Task OnInitializedAsync() { var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); user = authState.User; } private void IncrementCount() { currentCount++; } }
Источник: https://stackoverflow.com/questions/780 ... eping-data