簡體   English   中英

在 ASP.NET 內核中,如何知道用戶滿足最低年齡?

[英]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 的位置。所以似乎永遠不會滿足要求?

auth 聲明強化了最低要求,其中登錄用戶上下文用於檢查出生日期並與最低年齡要求進行比較。 這是代碼片段。

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/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM