Basic Authentication in Swagger (Open API) .Net 5

In this article we are going to learn about the how to setup the basic authentication for our API's using swagger in ASP.NET 5.0. As we all know, its newly launched Framework officially released in November. Here I am sharing the link to install the SDK for .Net 5



Packages used in this Project!

Swashbuckle.AspNetCore (latest version)

The above package is installed by default if you are going with ASP.NET Core 5.0 Step 1

Create a Project in Visual Studio and make sure to go with the same process from the image shown below.




Step 2

Add the Services Folder where we can add the business logic in that.



For the time being I have hardcoded the username and password in the code itself if you have the database with all the login credentials you can validate with that table.

UserService.cs

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Threading.Tasks;  
 
namespace BasicAuth.Services  
{  
 public class UserService : IUserService  
    {  
 public bool ValidateCredentials(string username, string password)  
        {  
 return username.Equals("admin")