Search code examples
c#antiforgerytokenasp.net-core-7.0

How to change Antiforgery cookie path in .NET Core 7


when getting the Antiforgerytoken cookie for my .NET Core 7 app, the path default to PathBase which is different from the root path in my case (/)

How can I change it?

I tried with:

builder.Services.AddAntiforgery(options =>
{
    options.Cookie.Path = "/";
});

when configuring services but no success. Even when that instruction is executed, it has no effect.


Solution

  • This code should set the Path property of the anti-forgery token cookie to the root path ("/"). Make sure that this configuration is placed in the ConfigureServices method before other configurations that might depend on it.

    If you've already tried this and it's not working, ensure that there are no conflicting configurations or middleware that might override this setting. Additionally, clear any cached cookies in your browser and restart your application to ensure the change takes effect.

    If you continue to experience issues, please double-check your middleware order and ensure there are no conflicting settings or overrides in your application.