How can i use an Antiforgery Token in ASP.NET Core 6 Web API with an extern consumer like a iOS or Android App? I don’t need user authentication for the requests. The app is hosted on another domain.
I have developed an Web API with an Antiforgery Token (Followed this link) and ASP.NET 6 Razor Pages. All is working perfekt. But how can i develop an extern App that uses this Web API? The Problem, i have no idea how can i create the Antiforgery Token from the "external" App? How can i configure the App to use the Web API with the Antiforgery Token?
3
Answers
it is not necessary to implement Anti-Forgery Token protection against CSRF Attacks when building an API because how APIs are built and intended to be used, they require different methods of protection like:
because the goal is to prevent malicious clients from calling our API, we need to validate the identity of the client app that performs the request to the API.
and in order to perform a CSRF attack, one of the main conditions is to have a Cookie-based authentication session (have a look at this article where it explains in detail how CSRF attacks are performed), which is not the case with APIs.
however, if you’re calling your API using Ajax from your website where the API is on the same origin as the website and you rely on Cookie to authenticate the user, it is possible to (and you should) integrate Anti-Forgery Token protection, you can check this Answer on StackOverflow for more details on how to implement it.
but since you are going to call the API from an external app just go with one of the above methods.
check this article on Microsoft docs for more details on CSRF Attacks and how Anti-Forgery Token protection is implemented.
also, check this article from RedHat to get more information about API security.
I don’t agree with the answer that "it is not necessary to implement Anti-Forgery Token protection against CSRF Attacks when building an API".
There is still a risk that somehow intruder can force the client app to send a malicious request.
To configure Anti-Forgery Protection in .NET Web API (without using MVC Views), you need to use the package
Microsoft.AspNetCore.Antiforgery
.Create a validation middleware:
Configure DI in your Web API application:
Configure a validation middleware:
Create a XSRF Token endpoint:
On the client make a request to the URL
/api/xsrf-token
.Then read a request token cookie
XSRF-TOKEN
and set it to aX-XSRF-TOKEN
HTTP header for non-GET requests:I was a security champion some time ago so know what is security (however recently not following the topic). But in every conference or training usually 2 things true:
In your case the problem is that Antiforgery Token (CSRF protection) is not available in APIs. It is available in MVC only and for a good reason:
So the answer is to your question another question. Do you use Cookie based Authorization in your API?
If not then you should NOT use Antiforgery tokens it is NOT impacting your API… Otherwise you should switch to Bearer Auth headers and you are set.
Also please ignore the coded answers because they have many issues. Implementation will compile, code runs for sure but full of security problems (Homebrew security). E.g. Token stored in Cookie which is the basic of CSRF attack Cookies attached by the browsers automatically to a request, GET method is secure and cannot even protected against, etc…
Homebrew security can give false confidence (yes I "secured" it) is it secured?: