Basic Auth for .NET Core
Utilising Basic Auth with .NET Core Identity
Updated: 03 September 2023
.NET Core Identity using Local Users can be used natively on application endpoints, this however makes use of a few additional layers of abstracting which can make using it with a WebAPI kind of troublesome
In order to add your own custom authentication which makes use of Basic Auth and the UserManager Dependency provided by Identity, you can do the following
In your API Class, define the following private property
Next, in your Controller’s constructor
you need to add the UserManager
dependency
In your application class, you can then provide a function which will verify the user login based on the HttpContext
of the request,
however this can also be implemented as a free-standing function which takes the username
, password
, and Usermanager
as inputs and works purely by making use of those. But for this context this function is defined inside of the controller class. This will return a bool
which indicates if the user making the request has been authenticated
The above can then be implemented by a controller with something like: