ASP.NET Core is a popular structure. Its key advantages include cross-platform execution, high performance, built-in dependency injection, and a modular HTTP request pipeline.

The ASP.NET Core supports many authentication providers to secure applications through numerous authentication workflows. However, in many scenarios, we have to provide a web application/site that is based on an unauthenticated API with anonymous access.

What to do

For example, we have a list of products in the database and we want to display these products on a web page. We can write an API to provide a list of products and the front end (website) can receive this list through the API and display it on our public products web page. Without applying a level of security, such architectures can be an open security vulnerability to exploitation.

Available Security Controls in ASP.NET

ASP.NET Provides solutions for common vulnerabilities including core

  • Cross-site scripting
  • SQL injection,
  • Cross-Site Request Forgery (CSRF)
  • Open redirects
Going a step further

As developers, we should also protect our applications from other common attack vectors including

  • Distributed denial-of-service (DDOS)
  • Denial-of-service (DOS)
  • Bulk data egress
  • Probe response
  • Scraping

The two steps we can take care of to verify the referrer header and rate-limiting, are discussed below in detail.

Use IP-based request limit action filter

We can limit customers to a certain number of requests over a specified period of time to prevent malicious bot attacks. We have created an IP-based request limit action filter in the ASP.NET Core. Keep in mind that multiple clients can sit behind a single IP address so you can meet this within your limits, or combine the IP address with other request data to make requests more unique.

To try the filter, you just need to add an ActionAttribute at the top of the controller action.

Here is the implementation of the filter:

Add Referrer check action filter

To protect the API from abuse and to provide additional protection against Cross-Site Request Forgery (CSRF) attacks, security checks are performed on the request referrer header for each REST API request sent to the server.

This API validates where the request comes from. We have created a Referrer Check Action Filter in ASP.NET Core. It prevents access to tools like POSTMEN, REST client, etc. You just need to do is add an ActionAttribute to the top of the controller Action.

Here is the implementation of the filter

Add DoS Attack Middleware

If you have the auto scale configured, DOS attacks overwhelm your APIs, making them unauthorized and/or expensive. There are various ways to avoid this problem by request throttling. There is an option here to use intermediaries to restrict the number of requests from particulate client IP addresses.

Below is the code for DosAttackMiddleware.cs

Conclusion

An unauthorized API is open to abuse. We should prevent the explicit attack vector by adding additional code. Hopefully, this blog makes it easier to enforce these restrictions while making the lives of these attackers more difficult.

Leave a Comment