I have this code in an interactiveserver component:
<form action="Account/Logout" method="post">
<AntiforgeryToken />
<input type="hidden" name="ReturnUrl" value="@currentUrl">
<button type="submit" class="nav-link border-0 text-white"
@onclick="@Logout">
<LocalText Key="Logout"></LocalText>
</button>
</form>
I want to send this form to a microsoft default identity core endpoint:
accountGroup.MapPost("/Logout", async (
ClaimsPrincipal user,
SignInManager<AppUser> signInManager,
[FromForm] string returnUrl) =>
{
await signInManager.SignOutAsync();
return TypedResults.LocalRedirect($"~/{returnUrl}");
});
I configured antiforgery like this:
var app = builder.Build();
...
app.UseHttpsRedirection();
app.UseStaticFiles();
app.MapControllers();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseAntiforgery();
app.MapRazorPages();
app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode();
app.MapAdditionalIdentityEndpoints();
app.Run();
but when I want to logout this error is raised:
An unhandled exception occurred while processing the request.
AntiforgeryValidationException: The required antiforgery request token
was not provided in either form field “__RequestVerificationToken” or
header value “RequestVerificationToken”.
Microsoft.AspNetCore.Antiforgery.DefaultAntiforgery.ValidateRequestAsync(HttpContext
httpContext)BadHttpRequestException: Invalid anti-forgery token found when reading
parameter “string returnUrl” from the request body as form.
Microsoft.AspNetCore.Http.RequestDelegateFactory+Log.InvalidAntiforgeryToken(HttpContext
httpContext, string parameterTypeName, string parameterName, Exception
exception, bool shouldThrow)Stack Query Cookies Headers Routing AntiforgeryValidationException:
The required antiforgery request token was not provided in either form
field “__RequestVerificationToken” or header value
“RequestVerificationToken”.
Microsoft.AspNetCore.Antiforgery.DefaultAntiforgery.ValidateRequestAsync(HttpContext
httpContext)
Microsoft.AspNetCore.Antiforgery.Internal.AntiforgeryMiddleware.InvokeAwaited(HttpContext
context)
how to correct this error?