【发布时间】:2021-10-23 17:45:33
【问题描述】:
我有一个使用 Microsoft.AspNetCore.Identity 的 Blazor 服务器应用程序。用户进行身份验证(使用 IdentityServer),然后可以根据他们的角色查看页面。我以两种方式之一检查角色。在页面的开头:
@attribute [Authorize(Roles = "some_user_role")]
或在代码块中:
<AuthorizeView Roles="some_user_role">
</AuthorizeView>
在我的 Startup.cs 类中,我有这个:
public void ConfigureServices(IServiceCollection services)
{
//db connection stuff
services.AddDefaultIdentity<CustomUserContext>(options =>
options.SignIn.RequireConfirmedAccount = true)
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddClaimsPrincipalFactory<UserClaimsPrincipalFactory<CustomUserContext>>();
// do other stuff
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//other stuff
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
}
但是,当我使用我的凭据进行身份验证时,即使我帐户的 EmailConfirmed 为 false,我仍然可以访问需要“some_user_role”角色的内容。如何强制执行 EmailConfirmed?在用户确认之前,我是否必须删除用户角色?
谢谢
【问题讨论】:
-
您可能想编写自己的中间件来检查这一点。授权不知道用户最终是如何进行身份验证的。在身份验证过程中,您会检查确认电子邮件等内容。
标签: c# asp.net-core blazor blazor-server-side asp.net-core-identity