Posts

Showing posts from September, 2025

Using Cookies in ASP.NET Core apps

Image
cookies Using Cookies in ASP.NET Core apps What are cookies Cookies are small data files sent by websites that are stored in the browser Cookies are sent every time a request is sent to the server Cookies are useful for persisting user login, tracking user activity to create personalized experiences, etc. Demo We will create a simple TODO list web app using ASP.NET Core The server will save the todo list in the user’s browser using cookies Set cookie A server can set a cookie and send it in the response to the client’s browser The browser will update the cookie Each cookie has a unique key In our demo application, the server sets a cookie to store todos in the client’s browser using Response.Cookies.Append Response . Cookies . Append ( nameof ( Todo ) , JsonSerializer . Serialize ( todos ) ) ; Retrieve Cookie data Client Browser sends cookies to the server in every request The cookie data can be retrieved using Request.Cookies In our dem...

Cookie based authentication explained in ASP.NET Core

Image
cookie_auth Cookie based authentication explained in ASP.NET Core Cookie based authentication can be implemented in ASP.NET Core using the built in Authentication service and Authentication middleware Add cookie authentication service in DI container // Add Cookie Authentication service builder . Services . AddAuthentication ( CookieAuthenticationDefaults . AuthenticationScheme ) . AddCookie ( options = > { options . LoginPath = "/Account/Login" ; // Specify the path to the login page options . AccessDeniedPath = "/Account/AccessDenied" ; // Specify the path for access denied options . ExpireTimeSpan = TimeSpan . FromMinutes ( 60 ) ; // Set the cookie expiration time options . SlidingExpiration = true ; // Enable sliding expiration } ) ; AddAuthentication adds required services for authentication. It also specifies the default authentication scheme to be used for authenticati...

Create Middleware in dotnet

Image
dotnet middleware Create Middleware in dotnet What is middleware in ASP.NET 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 pat...

.NET dependency injection explained with example

dotnet DI .NET dependency injection explained with example Dependency Injection container (DI container) Services (like email service, database service) are used generally by the application logic to interface with the external systems DI container is a collection of dependencies (aka services) Application logic can request an instance of a required service from the DI container Service Lifetimes When requesting a service instance from a DI container A new service instance is created every time if it is a Transient service A single instance of a service is reused every time if it is a Singleton service A service instance is reused through out a http request if it is a Scoped service. Scoped service is applicable only in web applications Inversion of Control for Dependency Injection While adding a service to the DI container, the service signature can be mentioned as an interface. An example is shown below. services . AddSingleton < IMyServic...