Reputation: 123
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
Reputation: 11
You have to move app.UseAntiforgery()
after app.UseAuthentication()
and
app.UseAuthorization()
. This worked with me.
Upvotes: 1
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
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
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