0% found this document useful (0 votes)
71 views10 pages

ASP NET Core Middleware 1680267109

The document provides an overview of middleware in ASP.NET Core, explaining its role in processing HTTP requests and responses through a pipeline of components. It covers various types of middleware, including authentication, logging, compression, caching, and CORS, along with tips for effective usage. Additionally, it discusses the creation of custom middleware to extend functionality as needed.

Uploaded by

vskyfall
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views10 pages

ASP NET Core Middleware 1680267109

The document provides an overview of middleware in ASP.NET Core, explaining its role in processing HTTP requests and responses through a pipeline of components. It covers various types of middleware, including authentication, logging, compression, caching, and CORS, along with tips for effective usage. Additionally, it discusses the creation of custom middleware to extend functionality as needed.

Uploaded by

vskyfall
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

ASP.

NET Core
Middleware
In 8 Minutes!

Keivan Damirchi

Include:
● What is Middlewares?
● Request and Response Processing
● Authentication Middleware
● Logging Middleware
● Compression Middleware
● Caching Middleware
● CORS Middleware
● Costume Middleware
● Tips for working with middleware
Let's go
What is
Middleware?
In ASP.NET Core, a middleware is a component that sits
between the web server and your application, processing
incoming HTTP requests and outgoing responses.

Each middleware performs a specific task, such as authentication,


logging, or routing, before passing the request on to the next
middleware in the pipeline. The pipeline is a sequence of middleware
components that are executed in the order they are added.

Note
Middleware pipeline is a sequence of middleware components that are
executed in order to process an incoming HTTP request and generate a
response.

1
Flexible HTTP Request
and Response
Processing
When an HTTP request is received by the web server, it
passes through the middleware pipeline, and each
middleware component has the opportunity to inspect and
modify the request and response objects.

Middlewares are a flexible way to add functionality to your application


without having to modify your application's core logic. You can add or
remove middleware components to customize the pipeline and add new
functionality to your application.

Note
keep each middleware component small and focused, and to use
existing middleware components when possible.

2
Authentication
Middleware
app.UseAuthentication()
This middleware is responsible for authenticating the user by
checking their credentials, such as a username and password,
against a user database. It can be used to ensure that only
authorized users can access certain pages or resources in
your application.

This code would add the


authentication middleware
component to the pipeline
before any other
middleware components.

This ensures that authentication is performed before any other middleware


components process the request.

Tip
While middleware can be a powerful tool for adding functionality to your
application, don't overuse it. Only add middleware that is necessary for your
application's functionality.

To read more:
https://dev.to/dotnet/authentication-in-asp-net-core-59k8

3
Logging
Middleware
app.UseLogging()
This middleware is used to log incoming requests and
outgoing responses, as well as other relevant information
such as response times and error messages. This is useful
for monitoring and debugging your application, as well as
for performance analysis and auditing.

This code adds logging


middleware to your application.
You can customize the logging
format and output destination by
providing a logging
configuration.

Tip
The order in which you add middlewares to the pipeline matters, as each middleware component
can modify the request and response objects before passing them on to the next middleware.
Make sure you add middlewares in the correct order to achieve the desired behavior.

To read more:
https://www.infoworld.com/article/3625494/how-to-use-http-logging-in-aspnet-core-6.html

4
Compression
Middleware
app.UseResponseCompression()
This middleware compresses outgoing responses to reduce the
size of data that is sent to the client, resulting in faster page
load times and reduced bandwidth usage. This is especially
useful for applications that serve large amounts of data, such as
images or videos.
This middleware compresses outgoing responses to reduce the size of data that is
sent to the client, resulting in faster page load times and reduced bandwidth usage.

This is especially useful for applications that serve large amounts of data, such as
images or videos.

Tip
Use existing middleware when possible: ASP.NET Core provides a number of built-in
middleware components for common tasks, such as authentication and logging. Use these when
possible instead of writing your own custom middleware.

To read more:
https://canro91.github.io/2020/10/01/CompressResponses

5
Caching
Middleware
app.UseResponseCaching()
This middleware caches the response of frequently
accessed requests, reducing the load on your server and
improving response times. This is useful for applications
that serve static content or data that doesn't change
frequently.

This code adds response


caching middleware to your
application. You can customize
the cache settings by providing
a caching options object.

Tip
If your middleware requires dependencies, such as a database context or a logger, use
dependency injection to inject these dependencies into the middleware component.

To read more:
https://code-maze.com/aspnetcore-response-caching

6
CORS
Middleware
app.UseCors()
This middleware enables Cross-Origin Resource Sharing
(CORS), which allows web pages from different domains
to access resources from your application. This is useful
for building web applications that consume data from
multiple sources, such as APIs or third-party services.

This code adds CORS middleware


to your application. You can
customize the allowed origins,
headers, and methods by providing
a CORS policy object.

Tip
Each middleware should be responsible for a specific task, such as authentication or
logging. Don't try to do too much in a single middleware component, as this can lead to
code complexity and maintenance issues.

To read more:
https://geeksarray.com/blog/how-to-setup-cors-policies-in-aspnet-core-web-api

7
Costume
Middleware
You can create your own middleware, In this example, we've created a custom
middleware called ‘MyMiddleware’. It takes a RequestDelegate parameter in its
constructor, which is a reference to the next middleware in the pipeline. The
InvokeAsync method is where the middleware logic is implemented.

In this case, the


middleware simply
logs a message
before and after the
request is processed.
You can customize
the middleware to
perform any logic
you need.

To use the middleware, you need to add it to the middleware pipeline in your Startup.cs file:

Now the MyMiddleware


component is added to the
pipeline and will be executed
for every incoming request.

8
Close();

Keivan Damirchi

You might also like