Skip to content

Instantly share code, notes, and snippets.

@nmchenry01
Last active November 28, 2021 15:32
Show Gist options
  • Save nmchenry01/5810579e3239eecdb57e3f07f86fdc2c to your computer and use it in GitHub Desktop.
Save nmchenry01/5810579e3239eecdb57e3f07f86fdc2c to your computer and use it in GitHub Desktop.
public class HasScopeHandler : AuthorizationHandler<HasScopeRequirement>
{
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, HasScopeRequirement requirement)
{
// If user does not have the scope claim, get out of here
if (!context.User.HasClaim(c => c.Type == "scope" && c.Issuer == requirement.Issuer))
return Task.CompletedTask;
// Split the scopes string into an array
var scopes = context.User.FindFirst(c => c.Type == "scope" && c.Issuer == requirement.Issuer).Value.Split(' ');
// Succeed if the scope array contains the required scope
if (scopes.Any(s => s == requirement.Scope))
context.Succeed(requirement);
return Task.CompletedTask;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment