确认要验证的AuthorizationHandler是否已在Startup.cs中注册。
打开你的AuthorizationHandler并在HandleRequirementAsync()方法中进行检查:
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, TRequirement requirement) { // Implement your validation logic here ... // Check if the user is authorized if (context.User.HasClaim(claimType, claimValue)) { // User is authorized context.Succeed(requirement); } else { // User is not authorized context.Fail(); }
return Task.CompletedTask;
}
在上面的代码中,确保使用context.Fail()而不是context.Fail(requirement)来标记验证失败。这可能会导致无限循环。
如果仍然存在问题,请确认是否在每个 Razor 组件中使用了类型为@attribute [Authorize] 或 @attribute [Authorize(Roles="role1,role2")]的授权属性。如果使用的话,可以使用 MSAL .NET Core 进行授权而无需使用 @attribute [Authorize]。
如果您正在使用版本较老的ASP.NET Core Identity并希望在Blazor应用程序中使用,则需要使用最新版本的Identity,即版本3.0或更高版本。
如果您正在使用Blazor应用程序,则必须将您的应用程序更新为.NET 5或更高版本。这是因为Blazor WebAssembly在.NET 5中进行了太多改进,而这些改进是在之前版本中不可用的。