Last active
November 28, 2021 15:32
-
-
Save nmchenry01/5810579e3239eecdb57e3f07f86fdc2c to your computer and use it in GitHub Desktop.
HasScopeHandler from https://auth0.com/docs/quickstart/backend/aspnet-core-webapi/01-authorization#validate-scopes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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