Skip to content

Minimal API Endpoint gives "Invalid antiforgery token" error #56687

Closed
@iustin94

Description

@iustin94

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I have an API endpoint mapped to handle a Logout request.

image

In my client component, when I try to make a request to this endpoint I get the error:

image

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.

image

I tried making it work using the GetAndStoreTokens call, which seems to be the up to date API for this.
image

However when the request is posted, the same error says again:

image

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.
image

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions