Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Allow custom handling of antiforgery failures #8604

Merged
merged 1 commit into from
Oct 19, 2018

Conversation

poke
Copy link
Contributor

@poke poke commented Oct 15, 2018

This fixes the problem highlighted in dotnet/aspnetcore#3616 and adds support to allow custom handling of antiforgery validation failures. Right now, the ValidateAntiforgeryTokenAuthorizationFilter simply sets a BadRequestResult and there is no way to hook into this behavior and do anything else.

This introduces a new AntiforgeryValidationFailedResult that extends BadRequestResult but allows to be identified explicitly within always-running result filters (they need to be always-running in order to go around the short-circuiting behavior of the MVC filter pipeline).

An example for a filter that intercepts the result is included in the functional test and essentially looks like this:

public class RedirectAntiforgeryValidationFailedResultFilter : IAsyncAlwaysRunResultFilter
{
    public Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
    {
        if (context.Result is AntiforgeryValidationFailedResult result)
        {
            context.Result = new RedirectResult("http://example.com/antiforgery-redirect");
        }

        return next();
    }
}

When applied in the pipeline, this filter will intercept the AntiforgeryValidationFailedResult and replace it with something else.

@poke poke force-pushed the AntiforgeryValidationFailedResult branch from 0359a3c to 1ce8e41 Compare October 17, 2018 23:19
@poke poke changed the title WIP: Allow custom handling of antiforgery failures Allow custom handling of antiforgery failures Oct 17, 2018
@poke
Copy link
Contributor Author

poke commented Oct 17, 2018

Updated the PR for way better approach. I like this :) Thanks @pranavkm for your feedback!

Copy link
Contributor

@pranavkm pranavkm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rynowak \ @javiercn any concerns?


// Assert
Assert.Equal(HttpStatusCode.Redirect, response.StatusCode);
Assert.Equal("http://example.com/antiforgery-redirect", response.Headers.Location.AbsoluteUri);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Member

@rynowak rynowak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is cool!

To enable custom handling of antiforgery validation failures, use an
`AntiforgeryValidationFailedResult` which is just a `BadRequestResult`
but allows to be identified explicitly inside always-running result
filters using the `IAntiforgeryValidationFailedResult` marker interface.
@poke poke force-pushed the AntiforgeryValidationFailedResult branch from 1ce8e41 to aa3594c Compare October 18, 2018 16:48
@pranavkm
Copy link
Contributor

@dotnet-bot build OSX 10.12 Release Build please

@pranavkm pranavkm merged commit ddbe0fe into aspnet:release/2.2 Oct 19, 2018
@pranavkm
Copy link
Contributor

Thanks for the PR!

@poke
Copy link
Contributor Author

poke commented Oct 19, 2018

Thanks for the quick merge, and the feedback! 😊

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants