Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable the antiforgery token check in ASP.NET MVC Core 2

I am trying to avoid "AntiForgery" checking as it always fails when hosted from the 3rd party server. I am using ASP.NET Core 2.0 MVC application.

I added this option in the ConfigureServices function:

services
    .AddMvc()
    .AddRazorPagesOptions( options =>
    {
        options.Conventions.AuthorizeFolder("/Account/Manage");
        options.Conventions.AuthorizePage("/Account/Logout");
        options.Conventions.ConfigureFilter(new IgnoreAntiforgeryTokenAttribute());
    } );

But still I am getting this exception.

System.InvalidOperationException: The antiforgery token could not be decrypted.
System.Security.Cryptography.CryptographicException: The key {6fb328e7-4808-4b5d-b7dc-870d126e5ca4} was not found in the key ring.

Am I missing anything ?

like image 508
Adam Avatar asked May 21 '18 05:05

Adam


People also ask

Does Antiforgery need token?

If the attacker forges a login page and gets the credentials that way, what's the point of using the token to protect the real login page? The attacker would be able to login anyway using the userand password that he got. This answer is dangerously wrong. The tokens ARE necessary.

How is Antiforgery token validated?

Require antiforgery validation The ValidateAntiForgeryToken attribute requires a token for requests to the action methods it marks, including HTTP GET requests. If the ValidateAntiForgeryToken attribute is applied across the app's controllers, it can be overridden with the IgnoreAntiforgeryToken attribute.

What is Antiforgery token in ASP.NET MVC?

To help prevent CSRF attacks, ASP.NET MVC uses anti-forgery tokens, also called request verification tokens. The client requests an HTML page that contains a form. The server includes two tokens in the response. One token is sent as a cookie.


3 Answers

Add the IgnoreAntiforgeryToken attribute (Order must > 1000) to the razor page model:

For example:

namespace CWACpch.Pages
{
    [IgnoreAntiforgeryToken(Order = 2000)]
    public class CreateOrderModel : PageModel
    {
like image 93
Luca Ziegler Avatar answered Oct 23 '22 00:10

Luca Ziegler


Been looking around for how to disable the cookie, setting the Order does not seem to help for me, and trying to set it to all pages via below also did not work for me.

options.Conventions.ConfigureFilter(new IgnoreAntiforgeryTokenAttribute());

I eventually found article below which helps per deleting the cookie locally, at least. Add the line below in the Startup.cs Disable .AspNetCore.Antiforgery Cookie

services.AddAntiforgery(options => { options.Cookie.Expiration = TimeSpan.Zero;});
like image 37
DeltaPng Avatar answered Oct 23 '22 00:10

DeltaPng


As per my understanding you don't have to disable any thing. By default if you use asp net tag helper to create form element it will put anti forgery token

It is upto you to validate anti forgery token by the use [ValidateAntiforgeryToken] annotation in action method or globally define configuration to ValidateAntiforgeryToken which will make system to try validate anti forgery token

If you have not configured system as mentioned about the system won't validate anti forgery token and won't be problem for your situation

like image 22
Kapil Ghimire Avatar answered Oct 22 '22 23:10

Kapil Ghimire