Posts

Showing posts from July, 2023

Implement Rate Limiting In ASP.NET Core Web API

Image
Introduction In this article, you'll learn about rate limiting and how to apply it to your ASP.NET Core Web API. What is Rate Limiting? If you're building an API, it might be accessed by multiple clients. Sometimes, certain clients use the API excessively without any restrictions. However, if you need to control how often a specific client can use the API within a set timeframe, you can achieve this through Rate Limiting. Rate Limiting is like setting a speed limit on how many times someone can ask for something from a website or app in a certain amount of time. It means each person or device using the service can only ask for a certain number of things within a particular timeframe. Why do we need Rate Limiting? Rate Limiting is a useful tool for safeguarding against harmful bot attacks. Imagine a hacker sending bots to repeatedly request access to an API endpoint. This flood of requests can ove...

Custom Middleware in .Net core 6.0

Image
  We will now write a custom middleware to avoid inline middleware in Program.cs.  The custom middleware will be written to a different class. Please keep the following in mind when writing custom middleware. There are several components that must be included in the middleware class: RequestDelegate is a public constructor with a parameter. Invoke or InvokeAsync is a public method that should return a Task. HttpContext should be the first parameter With IApplicationBuilder, an extension method is created for exposing the Middleware. Dependency Injection can be used to populate additional parameters for the constructor and for Invok/InvokeAsync. Let's quickly dive into Visual Studio and start building a middleware. In this tutorial, I'll be utilizing the following tools: Visual Studio Community Edition 2022 (64-bit) .NET 6.0 Minimal Web API Swagger There are three different methods ...