commit
699cf9e103
|
@ -1,4 +1,4 @@
|
|||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using System.Globalization;
|
||||
using System.Security.Claims;
|
||||
|
||||
|
@ -13,20 +13,25 @@ class MinimumAgeAuthorizationHandler : AuthorizationHandler<MinimumAgeAuthorizeA
|
|||
_logger = logger;
|
||||
}
|
||||
|
||||
// Check whether a given MinimumAgeRequirement is satisfied or not for a particular context
|
||||
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, MinimumAgeAuthorizeAttribute requirement)
|
||||
// Check whether a given MinimumAgeRequirement is satisfied or not for a particular
|
||||
// context
|
||||
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context,
|
||||
MinimumAgeAuthorizeAttribute requirement)
|
||||
{
|
||||
// Log as a warning so that it's very clear in sample output which authorization policies
|
||||
// (and requirements/handlers) are in use
|
||||
_logger.LogWarning("Evaluating authorization requirement for age >= {age}", requirement.Age);
|
||||
// Log as a warning so that it's very clear in sample output which authorization
|
||||
// policies(and requirements/handlers) are in use
|
||||
_logger.LogWarning("Evaluating authorization requirement for age >= {age}",
|
||||
requirement.Age);
|
||||
|
||||
|
||||
// Check the user's age
|
||||
var dateOfBirthClaim = context.User.FindFirst(c => c.Type == ClaimTypes.DateOfBirth);
|
||||
var dateOfBirthClaim = context.User.FindFirst(c => c.Type ==
|
||||
ClaimTypes.DateOfBirth);
|
||||
if (dateOfBirthClaim != null)
|
||||
{
|
||||
// If the user has a date of birth claim, check their age
|
||||
var dateOfBirth = Convert.ToDateTime(dateOfBirthClaim.Value, CultureInfo.InvariantCulture);
|
||||
var dateOfBirth = Convert.ToDateTime(dateOfBirthClaim.Value,
|
||||
CultureInfo.InvariantCulture);
|
||||
var age = DateTime.Now.Year - dateOfBirth.Year;
|
||||
if (dateOfBirth > DateTime.Now.AddYears(-age))
|
||||
{
|
||||
|
@ -34,15 +39,19 @@ class MinimumAgeAuthorizationHandler : AuthorizationHandler<MinimumAgeAuthorizeA
|
|||
age--;
|
||||
}
|
||||
|
||||
// If the user meets the age criterion, mark the authorization requirement succeeded
|
||||
// If the user meets the age criterion, mark the authorization requirement
|
||||
// succeeded
|
||||
if (age >= requirement.Age)
|
||||
{
|
||||
_logger.LogInformation("Minimum age authorization requirement {age} satisfied", requirement.Age);
|
||||
_logger.LogInformation(
|
||||
"Minimum age authorization requirement {age} satisfied",
|
||||
requirement.Age);
|
||||
context.Succeed(requirement);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogInformation("Current user's DateOfBirth claim ({dateOfBirth}) does not satisfy the minimum age authorization requirement {age}",
|
||||
_logger.LogInformation("Current user's DateOfBirth claim ({dateOfBirth})" +
|
||||
" does not satisfy the minimum age authorization requirement {age}",
|
||||
dateOfBirthClaim.Value,
|
||||
requirement.Age);
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
|
@ -10,15 +10,20 @@ class MinimumAgePolicyProvider : IAuthorizationPolicyProvider
|
|||
public DefaultAuthorizationPolicyProvider FallbackPolicyProvider { get; }
|
||||
public MinimumAgePolicyProvider(IOptions<AuthorizationOptions> options)
|
||||
{
|
||||
// ASP.NET Core only uses one authorization policy provider, so if the custom implementation
|
||||
// doesn't handle all policies (including default policies, etc.) it should fall back to an
|
||||
// ASP.NET Core only uses one authorization policy provider, so if the custom
|
||||
// implementation
|
||||
// doesn't handle all policies (including default policies, etc.) it should
|
||||
// fall back to an
|
||||
// alternate provider.
|
||||
//
|
||||
// In this sample, a default authorization policy provider (constructed with options from the
|
||||
// dependency injection container) is used if this custom provider isn't able to handle a given
|
||||
// In this sample, a default authorization policy provider (constructed with
|
||||
// options from the
|
||||
// dependency injection container) is used if this custom provider isn't able
|
||||
// to handle a given
|
||||
// policy name.
|
||||
//
|
||||
// If a custom policy provider is able to handle all expected policy names then, of course, this
|
||||
// If a custom policy provider is able to handle all expected policy names then,
|
||||
// of course, this
|
||||
// fallback pattern is unnecessary.
|
||||
FallbackPolicyProvider = new DefaultAuthorizationPolicyProvider(options);
|
||||
}
|
||||
|
@ -37,7 +42,8 @@ class MinimumAgePolicyProvider : IAuthorizationPolicyProvider
|
|||
if (policyName.StartsWith(POLICY_PREFIX, StringComparison.OrdinalIgnoreCase) &&
|
||||
int.TryParse(policyName.Substring(POLICY_PREFIX.Length), out var age))
|
||||
{
|
||||
var policy = new AuthorizationPolicyBuilder(JwtBearerDefaults.AuthenticationScheme);
|
||||
var policy = new AuthorizationPolicyBuilder(
|
||||
JwtBearerDefaults.AuthenticationScheme);
|
||||
policy.AddRequirements(new MinimumAgeRequirement(age));
|
||||
return Task.FromResult(policy.Build());
|
||||
}
|
||||
|
@ -46,4 +52,4 @@ class MinimumAgePolicyProvider : IAuthorizationPolicyProvider
|
|||
}
|
||||
}
|
||||
|
||||
// dotnet user-jwts create --claim http://schemas.xmlsoap.org/ws/2005/05/identity/claims/dateofbirth=1989-01-01
|
||||
// dotnet user-jwts create --claim http://schemas.xmlsoap.org/ws/2005/05/identity/claims/dateofbirth=1989-01-01
|
||||
|
|
|
@ -10,5 +10,6 @@ public class GreetingsController : Controller
|
|||
{
|
||||
[MinimumAgeAuthorize(16)]
|
||||
[HttpGet("hello")]
|
||||
public string Hello(ClaimsPrincipal user) => $"Hello {(user.Identity?.Name ?? "world")}!";
|
||||
public string Hello(ClaimsPrincipal user) =>
|
||||
$"Hello {(user.Identity?.Name ?? "world")}!";
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue