Search code examples
c#asp.net-coreauthorizationclaims-based-identityasp.net-core-identity

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


I am following this tutorial on authorization in asp.net core: https://learn.microsoft.com/en-us/aspnet/core/security/authorization/policies?view=aspnetcore-3.0

The example creates a MinimumAgeRequirement and sets its age to 21. The problem is I can't find where in the code it sets the user's age to 21. So it seems like the requirement will never be met?


Solution

  • For adding claims to user:

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

    You can find the complete project with AgeRequirement, Handler, Claim and Authorize on HomeController.

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