microsoft / fluentui-blazor

Microsoft Fluent UI Blazor components library. For use with ASP.NET Core Blazor applications
https://www.fluentui-blazor.net
MIT License
3.78k stars 363 forks source link

Blazor uses a fallback authorization strategy, and the component is not rendered normally. #2826

Closed zuijiaoghb closed 2 days ago

zuijiaoghb commented 2 days ago

🐛 Bug Report

Blazor uses a fallback authorization strategy, and the component is not rendered normally.

💻 Repro or Code Sample

[root@cpzljc jtpjsapp]# cat Program.cs using Microsoft.AspNetCore.Components.Authorization; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; using Microsoft.FluentUI.AspNetCore.Components; using jtpjsapp.Components; using jtpjsapp.Components.Account; using jtpjsapp.Data; using Pomelo.EntityFrameworkCore.MySql.Infrastructure; using Microsoft.AspNetCore.Authorization; using jtpjsapp.Authorization;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container. builder.Services.AddRazorComponents() .AddInteractiveServerComponents(); builder.Services.AddFluentUIComponents(); builder.Services.AddDataGridEntityFrameworkAdapter();

builder.Services.AddCascadingAuthenticationState(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped<AuthenticationStateProvider, IdentityRevalidatingAuthenticationStateProvider>();

builder.Services.AddAuthentication(options => { options.DefaultScheme = IdentityConstants.ApplicationScheme; options.DefaultSignInScheme = IdentityConstants.ExternalScheme; }) .AddIdentityCookies();

//var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found."); //builder.Services.AddDbContext(options => // options.UseSqlite(connectionString)); builder.Services.AddDbContextFactory(opt => opt.UseMySql("server=;port=;database=;user=;password=" , new MySqlServerVersion(new Version(8, 0, 31)) ) // The following three options help with debugging, but should // be changed or removed for production. .LogTo(Console.WriteLine, LogLevel.Information) .EnableSensitiveDataLogging() .EnableDetailedErrors() ); builder.Services.AddDatabaseDeveloperPageExceptionFilter();

builder.Services.AddIdentityCore(options => options.SignIn.RequireConfirmedAccount = true) .AddRoles() .AddEntityFrameworkStores() .AddSignInManager() .AddDefaultTokenProviders();

builder.Services.AddRazorPages();

builder.Services.AddAuthorization(options => { options.FallbackPolicy = new AuthorizationPolicyBuilder() .RequireAuthenticatedUser() .Build(); });

builder.Services.AddSingleton<IEmailSender, IdentityNoOpEmailSender>();

// Authorization handlers. builder.Services.AddScoped<IAuthorizationHandler, RecbillIsOwnerAuthorizationHandler>();

builder.Services.AddSingleton<IAuthorizationHandler, RecbillAdministratorsAuthorizationHandler>();

builder.Services.AddSingleton<IAuthorizationHandler, RecbillManagerAuthorizationHandler>();

var app = builder.Build();

using (var scope = app.Services.CreateScope()) { var services = scope.ServiceProvider; var context = services.GetRequiredService(); context.Database.Migrate(); // requires using Microsoft.Extensions.Configuration; // Set password with the Secret Manager tool. // dotnet user-secrets set SeedUserPW

var testUserPw = builder.Configuration.GetValue<string>("SeedUserPW");

await SeedData.Initialize(services, testUserPw); }

// Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseMigrationsEndPoint(); } else { app.UseExceptionHandler("/Error", createScopeForErrors: true); // 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.UseRouting(); app.UseAuthentication(); app.UseAuthorization();

app.UseStaticFiles(); app.UseAntiforgery();

app.MapRazorComponents() .AddInteractiveServerRenderMode();

// Add additional endpoints required by the Identity /Account Razor components. app.MapAdditionalIdentityEndpoints();

app.Run();

[root@cpzljc Pages]# cat Login.razor @page "/Account/Login" @attribute [AllowAnonymous]

@using System.ComponentModel.DataAnnotations @using Microsoft.AspNetCore.Authentication @using Microsoft.AspNetCore.Identity @using jtpjsapp.Data

@inject SignInManager SignInManager @inject ILogger Logger @inject NavigationManager NavigationManager @inject IdentityRedirectManager RedirectManager

登录

登录

使用注册后的邮箱登录。


登录

忘记了你的密码?

{ ["ReturnUrl"] = ReturnUrl }))">注册新用户

再次发送邮箱确认

@*

Use another service to log in.


*@
@code { private string? errorMessage; [CascadingParameter] private HttpContext HttpContext { get; set; } = default!; [SupplyParameterFromForm] private InputModel Input { get; set; } = new(); [SupplyParameterFromQuery] private string? ReturnUrl { get; set; } protected override async Task OnInitializedAsync() { if (HttpMethods.IsGet(HttpContext.Request.Method)) { // Clear the existing external cookie to ensure a clean login process await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme); } } public async Task LoginUser() { // This doesn't count login failures towards account lockout // To enable password failures to trigger account lockout, set lockoutOnFailure: true var result = await SignInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: false); if (result.Succeeded) { Logger.LogInformation("User logged in."); //RedirectManager.RedirectTo(ReturnUrl); RedirectManager.RedirectTo("home"); } else if (result.RequiresTwoFactor) { RedirectManager.RedirectTo( "Account/LoginWith2fa", new() { ["returnUrl"] = ReturnUrl, ["rememberMe"] = Input.RememberMe }); } else if (result.IsLockedOut) { Logger.LogWarning("User account locked out."); RedirectManager.RedirectTo("Account/Lockout"); } else { errorMessage = "错误:无效的登录尝试。"; } } private sealed class InputModel { [Required] [EmailAddress] public string Email { get; set; } = ""; [Required] [DataType(DataType.Password)] public string Password { get; set; } = ""; [Display(Name = "Remember me?")] public bool RememberMe { get; set; } } } run: ![Image](https://github.com/user-attachments/assets/15f0504f-e482-4b42-af22-7e3c4269e8d8) ## 🤔 Expected Behavior ![Image](https://github.com/user-attachments/assets/9c30976e-e720-4629-8a2e-6ceb4d923fde) ## 😯 Current Behavior no error。 ## 💁 Possible Solution ## 🔦 Context ## 🌍 Your Environment * OS & Device: centos8 on PC * Browser [Microsoft Edge] * .NET 8.0.301 and Fluent UI Blazor library Version 4.10.2
vnbaaij commented 2 days ago

Sorry, but this is not an issue report we can work with.

Please provide us with a zip file or repository for the code and at least make the screenshot in a language that we can read.

Closing this one so you can create a new one.