`The required antiforgery header value "X-XSRF-TOKEN" is not present.` when `.AspNetCore.Antiforgery` cookie exists
Hello - we are using ASP.NET Zero (AbpZero) 12.4 with .NET 7.0.
We've had an issue for at least the past year that we're only just identified: in non-localhost environments, hitting one of the MVC views for the .Web.Host project (such as /Views/Ui/Login.cshtml or /Views/Ui/Index.cshtml) is introducing a cookie that starts with .AspNetCore.Antiforgery. into the client, and as long as that cookie exists, most API calls in our Angular app result in:
The required antiforgery header value "X-XSRF-TOKEN" is not present.
at Microsoft.AspNetCore.Antiforgery.DefaultAntiforgery.ValidateRequestAsync(HttpContext httpContext)
at Abp.AspNetCore.Mvc.Antiforgery.AbpValidateAntiforgeryTokenAuthorizationFilter.OnAuthorizationAsync(AuthorizationFilterContext context)
If this specific cookie is deleted and nothing else is changed, then requests begin to work again.
I've noticed this issue on and off over the past year without knowing why it was happening and would simply clear the local app data - including cookies - and it would magically work again. I'm only now noticing that it corresponds to this .AspNetCore.Antiforgery. cookie.
According to https://abp.io/docs/latest/framework/infrastructure/csrf-anti-forgery, it seems that ASP.NET's antiforgery tokens are applied to Razor pages (like Login/Index above) but that you've implemented your own for the sake of the APIs (appservices).
It seems to me like the ASP.NET Razor antiforgery token is being included and then is getting caught by your own antiforgery filter (AbpValidateAntiforgeryTokenAuthorizationFilter) and is perhaps being rejected or conflicting in some way.
Is it possible that your filter is erroneously handling the Razor antiforgery tokens, or that our system is misconfigured after a few years of version upgrades? Is there anything obvious we can double check to ensure that the tokens are not conflicting with each other?
Thanks!
My steps to reproduce that may be specific to our project:
- I freshly log in to our Angular app (https://[appdomain]/app/) and the client gets an
Abp.AuthTokencookie - I can perform any action hitting our APIs without issue
- I, in the same browser session, even in a new tab, navigate to
https://[appdomain]/aspnet/Ui/Login - Being on the same domain, this request to either view
.cshtmlunder/Ui/pushes an.AspNetCore.Antiforgery.[random letters]cookie into the same session as the Angular app - From now on, any request made by the Angular app fails and logs the exception from above
- I use the DevTools to delete the
.AspNetCore.Antiforgery.cookie in the Angular app's browser session - The API hit by the Angular app begins to work and again and stops logging the error above
Source: aspnetboilerplate/aspnetboilerplate