【问题标题】:In ASP.NET Core, how to know the user meets the minimum age?在 ASP.NET Core 中,如何知道用户满足最低年龄?
【发布时间】:2020-02-18 04:32:42
【问题描述】:

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

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

【问题讨论】:

    标签: c# asp.net-core authorization claims-based-identity asp.net-core-identity


    【解决方案1】:

    为用户添加声明:

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

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

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

    【讨论】:

      【解决方案2】:

      auth 声明强化了最低要求,其中登录用户上下文用于检查出生日期并与最低年龄要求进行比较。这是代码sn-p。

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

      【讨论】:

        猜你喜欢
        • 2021-12-23
        • 2019-09-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多