Mvc: Posting from one Razor Page to another results in a 400 Bad Request

Created on 22 Aug 2017  路  5Comments  路  Source: aspnet/Mvc

I noticed that the Razor Pages template that includes authentication makes use of a controller for logging out rather than, say, a logout.cshtml (e.g. https://github.com/DamianEdwards/RazorPagesSample/blob/master/RazorPagesWebApplication/Pages/Account/Logout.cshtml). I tried to change it to use a logout.cshtml page and discovered that posting from one page to another (setting the "action" attribute of a form) results in a 400 status code when the form is submitted.

I am interested in learning if this is by design, and if so, why? Does it have something to do with AntiForgery checks, which I appreciate is disabled if you explicitly set the action on a form element?

question

Most helpful comment

Thanks.

Do you know why a controller was included in the template?

All 5 comments

This is almost certainly a missing antiforgery token (logs would confirm that). Razor Pages always validates an antiforgery token on a POST.

If you're using the form tag helper, you can use asp-page="/some/other/page" for linking rather than action.

Thanks.

Do you know why a controller was included in the template?

I assume that you're talking about this one: https://github.com/aspnet/Templates/blob/dev/template_feed/Microsoft.DotNet.Web.ProjectTemplates.2.0/content/RazorPagesWeb-CSharp/Controllers/AccountController.cs

I'm actually not sure. @danroth27 do you know? Is this a mistake?

There's no UI for the logout action, so a page doesn't really make sense. We decided to use controller actions for endpoints that have no associated UI.

@davidfowl @DamianEdwards

Yep, this was by design.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mdmoura picture mdmoura  路  33Comments

MicahZoltu picture MicahZoltu  路  37Comments

angelsix picture angelsix  路  61Comments

dotnetjunkie picture dotnetjunkie  路  43Comments

eat-sleep-code picture eat-sleep-code  路  40Comments