Skip to main content

Posts

Showing posts with the label ASP.NET Core Web API

A Sample On HttpClientFactory Using Typed Client Technique In .Net Core Application

In .Net Core, using HttpClientFacotory is the recommended way to consume the Rest API. The HttpClientFactory can be implemented using the following techniques. Using HttpRequestMessage Object Named Client Typed Client Typed Client: Typed Client technique is to implement or create a separate entity or class file per API domain. So all Http communication implementations are registered in a specific class per API domain. In this approach, each entity or class will be injected with 'System.Net.Http.HttpClient' object by the 'HttpClienFacotry'. So in this approach, we will not use 'HttpClientFactory' directly like we did for 'Named Client' and 'Using HttpRequestMessage Object' techniques. In simple words for consuming one third-party rest API need to create a specific class for it, if we have 'n' number of rest APIs to consume in our project then we need to create 'n' number of classes to implement the HttpClientFactory logic specific

A Sample On HttpClientFactory Implementation Using Named Clients Technique In .Net Core Application

An Overview On Named Clients: In HttpClientFactory, the Named Clients technique is useful when an application has a requirement to consume multiple external API's. In the Named Client approach HttpClienFactory produces the HttpClient object specific to the domain. So if our requirement to consume multiple external domains then HttpClientFactory generates HttpClient object per domain based on their registration in a startup.cs file. So each external API domain needs to be registered in the startup.cs file with a specific 'Name' to that HttpClient. This name will be passed to HttpClientFactory to produce a HttpClient object with the specified configuration in the startup.cs file Here we have a configuration object to set time out for the expiration of the HttpClient object. Click here to learn more about an overview of HttpClientFactory Basic Implementation Sample On HttpClientFactory Using HttpRequestMessage Object Test 3rd Party API's: So to understand and implement a s

.Net Core Sample Example On HttpClientFactory Basic Implementation With HttpRequestMessage Object.

In this article, we are going to see the HTTP client factory's basic implementation technique to consume an API in our .Net Core application.  Click here to understand how HttpClientFactory works. Create A Sample API Project: Now let's create a .net core web API sample project in which we are going to consume another API(Third-party API) using HttpClientFactory. You can create your sample project using editors like Visual Studio 2019 or  Visual Studio Code . Test API To Consume: Let's consume a free Rest API to consume for our leaning process. There a lot of free developer API for learning purposes. Here we are going to use JSONPlaceholder. JSONPlaceholder: Guide :- https://jsonplaceholder.typicode.com/guide.html Endpoints:- 1.https://jsonplaceholder.typicode.com/users/1/todos (Todos endpoint) 2.https://jsonplaceholder.typicode.com/users/1/albums (Albums endpoint) 3.https://jsonplaceholder.typicode.com/albums/1/photos (Photos endpoint) 4.https://jsonplaceholder.typicode.

Why To Use HttpClientFactory In .Net Core

HttpClient instance was used to invoke or consume the external rest API by clients like console applications or web applications. What Is Socket?: A Socket is a system communication protocol providing a communication channel over TCP connection. In programming, terminology socket can be defined as the combination of Url and Port Number to make communication channels. HttpClient Working Flow: Let's assume that we have .Net Core application, which will consume an external API using HttpClient. The steps involved in communication are as follows: The user requests our application, in which we need to call an external API to serve results to the user. On receiving user request our application creates an instance of HttpClient. Next HttpClient looks for HttpRequestHandler object in the pool of HttpRequestHandlers. Then HttpClient picks up one of the HttpRequestHandler objects and gives all input information like Url, payload, tokens, etc to make an external API call. Then HttpRequest

Refresh Token For JWT(JSON Web Token) On Authenticating .Net Core Application

In  Part-1 .Net Core Authentication Using JWT(JSON Web Token) , we have discussed step by step implementation about generating authentication token using JWT(JSON Web Token). Now we will discuss the generation of refresh token and using refresh token we will fetch authentication token again on its expiration. This article will be the continuation of  Part - 1 . RandomNumberGenerator Instance: System.Security.Cryptography.RandomNumberGenerator will be used to generate a random number which will be used as a refresh token. Note: It is not a mandatory approach to use 'System.Security.Cryptography.RandomNumberGenerator'. You can use your own some secured technique to generate a unique token string or you can use GUID. Generate Refresh Token: Let's add a private method that returns a random unique key that we can use as a refresh token. Logic/AccountLogic.cs: private string GetRefreshToken() { var key = new Byte[32]; using (var refreshTokenGenerator = RandomNumberGenerator.C

.Net Core Authentication Using JWT(JSON Web Token)

.Net Core application has many different authentication techniques like Cookie Authentication, Microsoft Identity Library, Identity Server 4, JWT, OAuth, etc. For a single page application that uses .Net Core Web Rest API, JWT is one of the simple and best approaches for performing token-based authentication. Here we going to understand the integration of JWT authentication for .Net Core API with a sample implementation. Overview On JWT: JSON Web Token is a digitally signed and secured token for user validation. JWT is constructed with 3 informative parts like: Header Payload Signature Create A Sample .Net Core API: Let's create a sample .Net Core API application to implement the JWT authentication. You can use an editor like Visual Studio 2019(Supports .Net Core 3.0 plus) or  Visual Studio Code . Add User.cs File: Let's add a User.cs file that represents a model of the user table(table will be integrated later steps). Data/Entities/User.cs: namespace JwtApiSample.Data.Enti