A request received to a ASP.NET web application is encapsulated as a HttpContext object and the object is processed by a series of application logic (aka middleware) in a configured sequence.
This sequence of middleware in which the requst is processed is called Request Pipeline
Use, Map, Run methods
Use, Map and Run methods help us to configure the middleware pipeline in an ASP.NET Core application
Use method appends a new middleware to the middleware pipeline
// Use Middleware: Append middleware to pipeline
app.Use(async(context, next)=>{
Console.WriteLine("Executing middleware before next()");await next.Invoke();// Calling the next middleware in the pipeline
Console.WriteLine("Executing middleware after next()");});
Map method branches the middleware pipeline for a specific request path
// Map Middleware: split middleware pipeline for a specific route
app.Map("/admin", adminApp =>{
adminApp.Use(async(context, next)=>{
Console.WriteLine("Admin Area Middleware: Before next()");awaitnext();
Console.WriteLine("Admin Area Middleware: After next()");});
adminApp.Run(async context =>{await context.Response.WriteAsync("Welcome to the Admin Dashboard!\\n");});});
Run method terminates the middleware pipeline
// Run Middleware: terminates the middleware pipeline
app.Run(async context =>{await context.Response.WriteAsync("From app.Run method");});
Create a Middleware class by implementing IMiddleware
A middleware class can be created as shown below. It should extend the IMiddleware interface
publicclassCustomMiddleware(ILogger<CustomMiddleware> logger): IMiddleware
{publicasync Task InvokeAsync(HttpContext context, RequestDelegate next){
logger.LogInformation("Before request from custom middleware");awaitnext(context);
logger.LogInformation("After request from custom middleware");}}
Requested Dependencies can be injected in the constructor
The middleware should be registered in the dependency container as shown below
Now middleware can be registered using builder.Services.AddCustomMiddleware() and middleware can be added to middleware pipeline using app.UseCustomMiddleware()
Create a Middleware class by conventional method (not recommended)
A middleware class can be created as shown below. The class should have
RequestDelegate in the constructor
public async Task InvokeAsync(HttpContext context) method in class
await next(context) should be called inside the InvokeAsync method
publicclassCustomMiddleware(RequestDelegate next){publicasync Task InvokeAsync(HttpContext context, ILogger<CustomMiddleware> logger){
logger.LogInformation("Before request from CustomMiddleware");awaitnext(context);
logger.LogInformation("After request from CustomMiddleware");}}
The middleware should be added to request pipeline using the following
app.UseMiddleware<CustomMiddleware>();
This style of creating a middleware class is not recommended since the arguments of InvokeAsync changes with injecting dependencies.
claimsprincipal_dotnet Authentication means identifying a subject (user, external application etc) ASP.Net Core Identity is a framework that uses claims based authentication to authenticate users Key concepts in Claims-based authentication Claim Claim is a statement about the subject (user) being authenticated It is a key value pair (like username=John is a claim, email=john1543@gmail.com is a claim, userId=236 is a claim) A claim can have an issuer (for example: userId=236 as per system , date of birth=1st-Jan-2006 as per Gmail ; here system and Gmail are the issuers) ClaimsIdentity ClaimsIdentity is a collection of claims of the subject (user) For example, driving license of a subject (user) can be a ClaimsIdentity that has claims like name, photo, date of birth, address etc. Passport can be a ClaimsIdentity that has claims like name, place of birth, photo, nationality, permanent address etc ClaimsIdentity also has a string property called Authenti...
dotnet_packages By using packages , third party logic can be used in dotnet applications Packages declaration in csproj file The external packages used by a project are declared in its csproj file like the following The packages will be downloaded and linked as per the csproj package references during the restore or build process Browse Nuget packages online Nuget packages can searched online at https://www.nuget.org/PACKAGES Manage packages from dotnet cli Install / update a package command - dotnet add <PROJECT_FOLDER> package <PACKAGE_NAME> -v <VERSION> The following command adds/updates a package in a project. If the package is already present, it will be updated to the latest version dotnet add package Microsoft.Data.Sqlite Install a specific version using -v flag like the following dotnet add package Microsoft.Data.Sqlite -v 8.0.10 List all packages in a project command - dotnet list <PROJECT_FOLDER> package ...
implicit usings Implicit usings = no repeated using statement in all files With implicit usings feature in a C# project, common using statements are automatically added by the compiler in all the C# files. For example using System; statement is not required to run the dotnet console application as shown below Console . WriteLine ( "Hello World!" ) ; Additional implicit usings can be declared in a C# project with global usings statements in any one of the project’s C# files as shown below. global using Ardalis . GuardClauses ; global using MediatR ; global using Project2 . Utils ; Existing implicit usings can be removed or included in the .csproj file of the C# project as shown below < ItemGroup > < Using Remove = " System.Threading.Tasks " /> < Using Include = " System.Math " /> </ ItemGroup > The implicit usings added in a C# project is based on the project type as sh...
Comments
Post a Comment