在 ASP.NET Core 中,如何知道用户是否满足最低年龄要求?

In ASP.NET Core, how to know the user meets the minimum age?

我正在关注 asp.net 核心中的授权教程:https://docs.microsoft.com/en-us/aspnet/core/security/authorization/policies?view=aspnetcore-3.0

该示例创建一个 MinimumAgeRequirement 并将其年龄设置为 21。 问题是我在代码中找不到将用户的年龄设置为 21 岁的地方。所以似乎永远不会满足要求?

身份验证声明强化了最低要求,其中登录用户上下文用于检查出生日期并与最低年龄要求进行比较。这是代码片段。

var dateOfBirth = Convert.ToDateTime(
            context.User.FindFirst(c => c.Type == ClaimTypes.DateOfBirth && 
                                        c.Issuer == "http://contoso.com").Value);

为用户添加声明:

await _userManager.AddClaimAsync(user, new Claim(ClaimTypes.DateOfBirth, myDateOfBirth));

您可以在 HomeController 上找到包含 AgeRequirement、Handler、Claim 和 Authorize 的完整项目。

https://github.com/zubairrana/CustomClaimValidation/