Skip to content

PageFilter and on page authorization check example

asyasky edited this page Jan 11, 2019 · 1 revision
    public override async Task OnPageHandlerExecutionAsync(PageHandlerExecutingContext context, PageHandlerExecutionDelegate next)
    {
        AuthorizationResult authResult;

        switch (this.EventRole)
        {
            case (EventRole.admin):
                authResult = authorizationService.AuthorizeAsync(User, "IsEventAdmin").Result;
                break;
            case (EventRole.author):
                authResult = authorizationService.AuthorizeAsync(User, "IsEventAuthor").Result;
                break;
            case (EventRole.play):
            default:
                authResult = authorizationService.AuthorizeAsync(User, "IsEventPlayer").Result;
                break;
        }

        if (authResult.Succeeded)
        {
            await base.OnPageHandlerExecutionAsync(context, next);
        }
        else if (User.Identity.IsAuthenticated)
        {
            context.Result = new ForbidResult();
        }
        else
        {
            context.Result = new ChallengeResult();
        }
    }
Clone this wiki locally