如何确定用户是否启用了身份验证器

How determine if user has enabled Authenticator

我正在开发一个 ASP.NET 核心 3.0 应用程序,它使用身份框架来验证用户。数据源是 MS SQL 服务器数据库。

作为双因素身份验证,默认的 MS 身份验证器已配置并正常工作。

实际上,我有一些用户启用了身份验证器,而另一些(大多数)用户则没有。 那些没有的人,我想展示一个额外的菜单项。大多数菜单项都是根据角色构建的,并且运行良好。

框架是否可行,还是我必须自己查询用户table?

@using Microsoft.AspNetCore.Identity
@inject SignInManager<IdentityUser> SignInManager
@inject UserManager<IdentityUser> UserManager


@if (SignInManager.IsSignedIn(User))
{
    //...

    <a href="#" class="dropdown-toggle">Profile</a>

    //...

    @if (!_2faEnabled) //How to determine _2faEnabled?
    {
        <li><a href="~/Profile/Manage/EnableAuthenticator">Enable 2FA</a></li>
    }

    ///...
}

使用UserManager获取当前用户为:

var user = await UserManager.GetUserAsync(User);

然后从 user 对象中获取 TwoFactorEnabled 值。


注意: 如果 Enable 2FA 在主菜单中,例如:主页 |项目 |任务 |启用 2FA | ..ETC。那么 IMO 这将是一个问题,因为您会访问数据库 every-time 用户浏览您的网站。所以你可能会考虑在第一次从数据库中获取 user.TwoFactorEnabled 的值后将其存储在某处。

而如果 Enable 2FA 在特定页面内,例如:Manage 2FA 并且在其中您会得到 the current user 然后检查 TwoFactorEnabled 就不会那么糟糕(因为用户不会经常访问此页面)。