Skip to main content

Posts

Blazor Server Authentication And Authorization Using Microsoft AspNetCore Identity[.NET6]

In this article, we are going to do a small demo on Blazor Server authentication and authorization using Microsoft AspNetCore Identity. Microsoft AspNetCore Identity Library: AspNetCore Identity: Built-in login functionality library that supports authentication of all different .NET applications. Provides rich authentication UI pages and customizable as well. Adoptable for External Authentication providers like 'Google', 'Facebook', 'Outlook'. Can be integrated with other authentication like 'IdentityServer4', 'Azure Active Directory', 'Azure Active Directory B2C(Azure AD B2C)'. Create A .NET6 Blazor Server App With Individual Authentication: Let's create a .Net6 Blazor Server sample application with individual authentication to accomplish our demo. We can use either Visual Studio 2022 or Visual Studio Code(using .NET CLI commands) to create any.Net6 application. For this demo, I'm using the 'Visual Studio Code'(using

Blazor Server CRUD Operation Using MudBlazor UI Components[.NET6]

In this article, we are going to implement CRUD operation in .NET6 Blazor Server application using the MudBlazor UI components. Blazor Server: Blazor Server is a single-page application. At the server, a pre-rendered HTML output of the page will be delivered to the user browsers. Any UI update, event handling, javascript calls will carry over by a SingnalR connection to the server. So application updates are depending on the continuous connection of the SignalR. So Blazor server is a single-page application that can be made of C#. Since the blazor server only outputs the pre-rendered HTML to the client, so there is no c# code downloading into user browsers like in Blazor WebAssembly(c# code downloaded and run in the browser for blazor webassembly application). SignalR Connection: A Blazor Server application works over a SignalR connection. A Blazor Server application creates a UI State memory at the server which will be interacted by the SignalR connections. If a SignalR conn

HTTP Logs With 'UseHttpLogging' Middleware[.NET6 Feature]

In this article, we will know about a .NET6 feature that is 'UseHttpLogging' middleware for logging HTTP requests and responses. UseHttpLogging Middleware: The .NET6 providing a built-in middleware for HTTP logs that is 'UseHttpLogging'. So this middleware provide log information on 'HTTP Request and Response Information', 'Headers' and 'Request Body Information', etc. Create A .NET6 Application: This 'UseHttpLogging' middleware can be configured to any .NET application like MVC, Razor Pages, API, etc. Let's create a .Net6 API sample project to accomplish our demo. We can use either Visual Studio 2022 or Visual Studio Code(using .NET CLI commands) to create any.Net6 application. For this demo, I'm using the 'Visual Studio Code'(using the .NET CLI command) editor. CLI command For Minimal API Project dotnet new webapi -o Your_Project_Name Configure UseHttpLogging Middleware: Now configure the 'UseHttpLogging'

Minimal API 'Result.Stream()' Return Type[.NET6]

In this article, we will know about a Minimal API return type that is 'Result.Stream()' Result.Stream(): The 'Result.Steam()' can be used to deliver the stream of data as a response from the Minimal API endpoint. For example on consuming a third-party API from our minimal API endpoint, a common approach is to read the response from the third-party API and then deserialize the response and finally return the response using 'Result.Ok()'. But using 'Result.Stream()' return type we can directly return the stream of third-party API response without deserializing. Create A .NET6 Minimal API Project: Let's create a .Net6 Minimal API sample project to accomplish our demo. We can use either Visual Studio 2022 or Visual Studio Code(using .NET CLI commands) to create any.Net6 application. For this demo, I'm using the 'Visual Studio Code'(using the .NET CLI command) editor. CLI command For Minimal API Project dotnet new webapi -minimal -o Your_

Different HttpClient Techniques To Consume API Calls In Minimal API[.NET6]

In this article, we are going to implement different HttpClient techniques to consume API calls in minimal API. The different HttpClient techniques that we are going to explore are like: Register HttpClient Object Explicitly In DI(Dependency Injection Service) Named Client Type Client HttpRequestMessage Object Create A .NET6 Minimal API Project: Let's create a .Net6 Minimal API sample project to accomplish our demo. We can use either Visual Studio 2022 or Visual Studio Code(using .NET CLI commands) to create any.Net6 application. For this demo, I'm using the 'Visual Studio Code'(using the .NET CLI command) editor. CLI command For Minimal API Project dotnet new webapi -minimal -o Your_Project_Name Create A Third Party API Response Model: Here I'm going to use a free third-party rest API that is "https://jsonplaceholder.typicode.com/posts". So to receive the response let's create a response model like 'Post.cs'. Program.cs:(Add Post.cs c

Implement IResult For Custom Response In Minimal API[.NET 6]

In this article, we will understand the implementation of IResult for custom response in minimal API[.NET6]. IResult Type: In minimal API to return a custom response, we have to implement the 'Microsoft.AspNetCore.Http.IResult'. class CusomtResult : IResult { public Task ExecuteAsync(HttpContext httpContext) { throw new NotImplementedException(); } } The 'ExecuteAsync' method gets automatically invoked, the only parameter it will have is the 'HttpContext' where we can use to append our custom response type. Create A .NET6 Minimal API: Let's create a .Net6 Minimal API sample project to accomplish our sample. We can use either Visual Studio 2022 or Visual Studio Code(using .NET CLI commands) to create any .NET6 application. For this demo, I'm using the 'Visual Studio Code'(using .NET CLI commands) editor. CLI command For Minimal API Project dotnet new webapi -minimal -o Your_Project_Name Implementing Custom Response W

.NET Application Controller Actions Now Support Asynchronous Streaming Response[.NET6 Feature]

In this article, we will know about .NET6 feature that is MVC/API controller actions now supports the asynchronous streaming response. Async Stream: A data stream is often retrieved or generates elements asynchronously is 'Async Stream'. Now from .NET6 we can output the async stream of response from the controller's actions. An action method that contains return type 'IAsyncEnumerable' no longer buffers the response content in application memory before it sends to the clients. So not buffering help to reduce the application memory usage when returning large dataset result that can be asynchronously enumerated. Sample Action Method That Returns 'IAyncEnumerable<T>' Results: Let's write a sample code that can return the asynchronous stream of data. [Route("Test")] [HttpGet] public async IAsyncEnumerable<int> GetNumbers() { for (int i = 1; i < 100; i++) { await Task.Delay(10); Console.WriteLine(i); yield return i;