Anti Forgery with Blazor Server App

Balu Raju 81 Reputation points
2024-02-09T16:57:41.2+00:00

we upgraded our blazor server app from .Net 6 to .Net 8 following the instructions from https://free.blessedness.top/en-us/aspnet/core/migration/70-80?view=aspnetcore-8.0&tabs=visual-studio Below is the snippet from the Program.cs where app.UseAntiForgery() is called. Our Blazor app calls several API calls hosted separately on the IIS server as the Blazor Server App is hosted on. While this works in VS IDE using IIS express (localhost), it does not work when it is hosted on the IIS Server. I see the following error in the event log. "The Anitforgery Token could not be decrypted. User's image

In the Development tool I see the following User's image

Code snippet from Program.cs app.UseHttpsRedirection(); app.UseSession(); app.UseStaticFiles(); app.UseAuthentication(); app.UseAuthorization(); app.UseRouting(); app.UseAntiforgery(); app.MapControllers(); app.MapRazorComponents<App>() .AddInteractiveServerRenderMode(); app.Run();

Developer technologies | .NET | Blazor
{count} votes

2 answers

Sort by: Most helpful
  1. Balu Raju 81 Reputation points
    2024-02-09T20:26:33.2633333+00:00

    the two errors that I mentioned above are unrelated. Even though I get the error about AntiForgery Token that was not preventing the app from loading. It was the error that I mentioned from the DevTools. Strangely that due to Case Senstive Url. The original application URL as it is deployed in the IIS structure is "https://mysite.com/Portal", but I was trying with https://mysite.com/portal. Once I ensured it takes right case URL, the error is gone and the application loaded successfully. Now this Case -Sensitive URL could be a problem that my clients may not be happy with but at least I know that is the issue. SO Anti forgery token issue is really not the issue even though it was an error but still loading the app. Any suggestion on making URL Case-INsensitive.


  2. Balu Raju 81 Reputation points
    2024-02-09T20:43:54.8+00:00

    No the URL is generated by the Deployment tool we use to deploy/host the application on the IIS server to give flexibility to our clients who deploy the app on their side. Depending on how/where they host - whether they host it directly under the site or under sub directory, etc. So the URL is not hardcoded in our application code. When I say it is case-sensitive I'm only talking about the sub directory portion in the URL. the domain part is fine to be case-insensitive. https://mysite.com/Portal vs https://mysite.com/portal


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.