【问题标题】:Blazor and Microsoft.AspNetCore.Identity Requiring EmailBlazor 和 Microsoft.AspNetCore.Identity 需要电子邮件
【发布时间】: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


【解决方案1】:

几乎是的。电子邮件确认与帐户工作无关 - 可以重置,即更改电子邮件。

按照你的逻辑要求做任何事情。

【讨论】:

  • 谢谢。但是,那么 options.SignIn.RequireConfirmedAccount = true 用于 services.AddDefaultIdentity 是什么?
  • @jason 默认模板强制要求 RequireConfirmedAccount = true。但是,它也会绕过发送电子邮件并检查收据的代码,直到中间件设置完成。您会注意到,当您使用模板代码创建新帐户时,它只会为您提供一个链接来确认您的详细信息,而无需通过电子邮件流程。
  • 我不确定这是否正确,但我最终创建了自己的自定义授权要求/处理程序,类似于此处回答的内容:stackoverflow.com/questions/61959852/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多