Skip to content

Blazor antiforgery token issue when posting form (SSR) when user is logged in #50612

Closed as not planned
@sikora507

Description

@sikora507

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

When I log in a user using cookies, then when I want to post a form using server side rendering, I get this error page with the following text only:

A valid antiforgery token was not provided with the request. Add an antiforgery token, or disable antiforgery validation for this endpoint.

I've isolated the issue and made an example demo application that shows the problem:
https://github.com/sikora507/blazor-form-submission-bug

I've made a simple blazor page with simple form that I want to post using SSR, and as you can see, I am not logged in, and I am able to submit a form, and it's content is displayed below. There is no issue yet.
obraz

However, when I log in and want to post the same form:
obraz
When I press the Submit button, I'll get this page:
obraz

For logging in, I am using Razor Pages (Login.cshtml and Logout.cshtml)
I am doing so, because in my real-world scenario, I want to login using Google and the issue is the same.

This is my actual code that signs in using Google. I need to support a Challenge to Google and a Callback where I sign in the user.
obraz

I've isolated the issue by mocking the logging process and calling HttpContext.SignInAsync with pre-made user:
https://github.com/sikora507/blazor-form-submission-bug/blob/4b4c0341fe5089b0ecbe83e5a5d6a44783815d53/FormTest/Pages/Login.cshtml.cs#L24C1-L34C29
But the outcome is the same, there's some weird behavior with Blazor's SSR form handling.

I might be wrong, but I was not able to mitigate this issue, I'd be also happy if someome could prove my mistake and show me how it's suppose to be done.

Expected Behavior

There should be no antiforgery token issue after posting a SSR form in Blazor, when user is logged in.

Steps To Reproduce

  1. Download and run repo that isolates the issue:
    https://github.com/sikora507/blazor-form-submission-bug

It was generated using dotnet new blazor command

  • a page with form was added
  • a razor page for signing a dummy user was made
  1. Click Login link
    obraz,
  2. Click
    obraz,
  3. Try to submit the form on Form test page
    obraz

You will get the error

A valid antiforgery token was not provided with the request. Add an antiforgery token, or disable antiforgery validation for this endpoint.

This issue does not occur when user is logged out. (Try logging out and submit the form again, it will work)

Exceptions (if any)

There is a bad request error in console:
obraz
Caused by blazor.web.js

.NET Version

8.0.100-preview.7.23376.3

Anything else?

No response

Metadata

Metadata

Assignees

Labels

area-blazorIncludes: Blazor, Razor ComponentsbugThis issue describes a behavior which is not expected - a bug.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions