Context:
I’m building an application using .NET 8 Blazor AUTO.
I have 2 projects (server & client), all of my components + my MainLayout are within my client project.
Problem statement
I like to load data that is relevant to all pages within my MainLayout. Whenever I include this code (& my guess would be perform the StateHasChanged();) my signout functionality returns an error.
MainLayout Data retrieval
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
// RETRIEVE DATA from service
Loading = false;
StateHasChanged();
}
}
Sign out functionality
<Authorized>
<div class="nav-item px-3">
<form action="Account/Logout" method="post">
<AntiforgeryToken />
<input type="hidden" name="ReturnUrl" value="@currentUrl" />
<button type="submit" class="nav-link">
<span class="bi bi-arrow-bar-left-nav-menu" aria-hidden="true"></span> Logout
</button>
</form>
</div>
</Authorized>
The error
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)
Program.cs (server)
I’m including my program.cs configuration. I have tried every possible sorting order, always the same error. (I don’t have any registration in my client project program.cs)
app.UseAuthentication();
app.UseAuthorization();
app.UseAntiforgery();
Whenever I move my data retrieval away from my MainLayout to every individual page (which I don’t like to do) and delete the StateHasChanged() in my MainLayout the sign-out functionality simply works.
I’ve been googling for days but can’t find any relevant information / solutions. Anyone can help me out? Much appreciated!