-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Blazor antiforgery token issue when posting form (SSR) when user is logged in #50612
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
If you haven't yet, take a look at Blazor .NET 8 Preview 7 antiforgery new requirement: .NET 8 Preview 7 Blog
|
I've just tried adding it, but still no difference. When I use cookies for authentication, SSR form submission seems to be broken. I've added a commit to my repo with your proposed changes too. |
Did you also add in app program.cs?: |
This is caused by the implicit antiforgery middleware, you can use |
Is there an existing issue for this?
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:
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.

However, when I log in and want to post the same form:


When I press the Submit button, I'll get this page:
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.

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
https://github.com/sikora507/blazor-form-submission-bug
It was generated using
dotnet new blazor
commandYou will get the error
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:

Caused by blazor.web.js
.NET Version
8.0.100-preview.7.23376.3
Anything else?
No response
The text was updated successfully, but these errors were encountered: