Posts

Showing posts from 2023

SSL Certificate authentication in ASP.NET Core

Have you ever wondered, even though you added a certificate to HttpClient while accessing data from the Api service, why you are getting unAuthorized access or your Api is not working as expected? Answer to the above question is that the certificate exchange is done at the start of the HTTPS conversation, it's done by the server before the first request is received on that connection. So the right place to add a certificate to HttpClient is the Program.cs or StartUp.cs file. Microsoft.AspNetCore.Authentication.Certificate contains an implementation similar to Certificate Authentication for ASP.NET Core. Certificate authentication happens at the TLS level, long before it ever gets to ASP.NET Core. More accurately, this is an authentication handler that validates the certificate and then gives you an event where you can resolve that certificate to a ClaimsPrincipal. Implement an HttpClient using a certificate and IHttpClientFactory In the following example, a client certificate is

Publish an Angular with .NET CORE app to IIS on AWS Cloud EC2 instance

I faced a few challenges while publishing my application(Front end on Angular 13, Backend is .NET CORE 6) to IIS on AWS EC2 instance using Custom Identity. So I thought of sharing this post which will help you to fix the issue quickly. It is a straight forward process to publish an application to IIS without using custom identity and many are already aware of this process. When we create an Application Pool by default it uses Application Pool Identity. In many cases we can not use Application Pool Identity. For example if your application is accessing web share or network share then your application needs to have required permission to access these shares. In this case you will have to create a Service Account and provide necessary permission to your Service Account to access these shares. Then you need to use this Service Account as Identity instead of Application Pool Identity to set up your application. However if you want to use another identity then we have to make sure your new i

Handle errors in ASP.NET Core web APIs

Image
Handle errors in ASP.NET Core web APIs Exception handling is one of the critical areas in modern web application development. If exceptions are not handled properly, the whole app can be terminated, causing severe issues for users and developers. Try-catch blocks are widely used in apps to handle exceptions. They are the most basic way of handling exceptions. ​ The above example is a typical case where we use try-catch block to handle the exception. The try-catch block method is ideal for novice developers, and it is something that every developer should be aware of. However, this technique has a disadvantage when working with massive projects with complex architectures. In such cases we have to use Exception Handling Middleware to handle the exceptions. Exception handler In Program.cs, call UseExceptionHandler to add the Exception Handling Middleware: ​ Configure a controller action to respond to the /error route: ​ Add [ApiExplorerSettings] attribute and set its IgnoreApi property t