Dev Raj Gautam
Dev Raj Gautam

Reputation: 123

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

I created a Microsoft Teams app using Razor components and have configured the following it, but still receive an antiforgery token error. In Program.cs

app.UseAuthentication();
app.UseAuthorization();
app.UseAntiforgery();

app.MapRazorComponents<App>()
    .AddInteractiveServerRenderMode();

I have also added the tag <AntiforgeryToken/> to my .razor page.

Upvotes: 0

Views: 5364

Answers (4)

Youssef Salame
Youssef Salame

Reputation: 11

You have to move app.UseAntiforgery() after app.UseAuthentication() and app.UseAuthorization(). This worked with me.

Upvotes: 1

marzman95
marzman95

Reputation: 11

Are you using Interactive Server, WebAssembly, or both? I ran into a similar issue where I recently added builder.Services.AddServerSideBlazor(); to Program.cs. This apparently broke the generation of Antiforgery Tokens in both EditForm and <AntiforgeryToke />, which will cause errors when processing a form.

Upvotes: 1

SendETHToThisAddress
SendETHToThisAddress

Reputation: 3744

For me I had to add @formname and AntiforgeryToken to my form like this:

<form method="post" @formname="MyForm" @onsubmit="MyOnSubmitMethod">
    <AntiforgeryToken />
    ...additional form code here...
</form>

Adding a form name by other means did not work for me, I had to use @formname.

Upvotes: 0

Kenneth Castillo
Kenneth Castillo

Reputation: 11

try rename your FormName

<EditForm Model="@UserAuth" FormName="testForm" OnValidSubmit="Test">
    <InputText @bind-Value="UserAuth.Username" />
    <InputText @bind-Value="UserAuth.Password" />
    <button type="submit">Login</button>
</EditForm>

In some cases repeated names can give you this error

Upvotes: 0

Related Questions