Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Why is there no antiforgery token rendered?

$
0
0

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

The antiforgery token is not being rendered on the client:

<form action="Account/Logout" method="post" b-u3usycoz9l=""><!--!--><!--!--><input type="hidden" name="ReturnUrl" value="" b-u3usycoz9l=""><!--!--><!--!--><button type="submit" class="btn btn-link" b-u3usycoz9l=""><i class="fa-light fa-right-from-bracket" b-u3usycoz9l=""></i> Sign out</button></form>

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!


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>