-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Minimal API Endpoint gives "Invalid antiforgery token" error #56687
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
I forgot to mention that there was this issue #49929 which had a similar problem, but it was accepted as fixed with the conclusion being to not use antiforgery at all at the endpoint? |
@iustin94 thanks for contacting us.\ It's not clear that what you are doing is correct. When you say client component do you mean What might be happening in this case is that the antiforgery token might have been discarded if it wasn't consumed during the initial render of the app (we have a bug to fix it). You can try resolving the |
@javiercn So the client component is a blazor SSR rendered component. The GetAndStoreTokens works fine as I sayd previously, but the issue that I have is that when I make the request from my component, by using the instantiated HttpClient, the middleware throws an error saying that the request is bad. Here is the full code for my component. This is a .net8 project using Blazor Server Side. This is the code for the MainLayout component. In the navigation bar you can see I have both a Logout button and a Logout form element. When the form submits, everything seems to work as it should. When the Logout button is clicked on the other hand, the Logout method fails and gets the BadRequest error.
|
@iustin94 thanks for the additional details. So to make sure we understand. You have an SSR component and from within that component you are making an HTTP Client call to yourself? |
Yes, it is all in the "Logout" method of the component I posted above. |
@iustin94 thanks for the additional details. That's likely not going to work. You need to setup the HttpClient instance with the auth cookie you received on the request to the server + the antiforgery cookie + antiforgery request token in the header. Based on this, we don't think there's a bug here, just missing requirements on a scenario we don't directly support/recommend. I'm not sure why you are trying to log out via an additional API call to the server as opposed to logging out directly. The Identity endpoints are meant to be used by JS clients and native apps and not by an SSR app. |
This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes. See our Issue Management Policies for more information. |
Thank you for the input. The 3 conditions are what I though I was missing and am trying to understand why they are missing and am looking to see an example of how to set this up properly. I think at this point it's not a reason to other than on my part understanding the mechanism of how this works. Regarding the identity endpoint comment, I don't think I am using the ones you mention, the standard ones. I have some simple defined endpoints, and the logout one is the following:
Is this what you mean by endpoint mean to be used by JSClients? |
Is there an existing issue for this?
Describe the bug
I have an API endpoint mapped to handle a Logout request.
In my client component, when I try to make a request to this endpoint I get the error:
I tried making this work by following the documentation example at https://learn.microsoft.com/en-us/aspnet/core/blazor/call-web-api?view=aspnetcore-8.0#antiforgery-support but failed.
First, because the documentation example is outdated. The AntiforgeryRequestToken has no RequestToken field.
I tried making it work using the GetAndStoreTokens call, which seems to be the up to date API for this.

However when the request is posted, the same error says again:
As far as I can tell, the cookie is not being set in the HttpClient headers properly. I do set the token, but the cookie is missing.
I have tried setting the header to different names but nothing has worked so far.
The use of a
element however, works just fine, with the request passing.Expected Behavior
I expect to have the same bahavior possible through using a element or programmatically making an HttpClient and a request.
Steps To Reproduce
Follow the documentation. Create a template project, use the example code and it will not work.
Exceptions (if any)
No response
.NET Version
.net8
Anything else?
No response
The text was updated successfully, but these errors were encountered: