【发布时间】:2021-08-31 07:49:55
【问题描述】:
我在 Razor Pages 项目中使用 ASP.NET Core Identity。 如果经过身份验证的用户不符合特定策略(例如未启用 2FA),您如何重定向到特定页面(例如启用 2FA 页面)?
我希望避免在每个 OnGet 中检查声明,例如:
public IActionResult OnGet()
{
var claimTwoFactorEnabled = User.Claims.FirstOrDefault(t => t.Type == "TwoFactorEnabled");
if (claimTwoFactorEnabled != null && "true".Equals(claimTwoFactorEnabled.Value))
{
// You logged in with MFA, do the admin stuff
}
else
{
return Redirect("/Identity/Account/Manage/TwoFactorAuthentication");
}
return Page();
}
我确实找到了这个answer,但它似乎需要 OpenIdConnect。我正在使用独立的身份。
【问题讨论】:
-
你读过the docs吗?通常最好的地方也是先看。
-
那里有一个独立的身份示例,需要上面的代码块;我想看看是否有另一种解决方案不涉及检查每个 OnGet 中的声明。
标签: asp.net .net-core identity