Featured Article

ASP NET Web API Basic Authentication Example Step by Step Guide

Kenneth Jul 13, 2026

Asp.NET Web API, a framework for building HTTP services, often requires secure access to data. Basic authentication, the simplest form of access control, comes in handy for such needs. Let's dive into a hands-on example of implementing basic authentication in an Asp.NET Web API application.

Use Data Annotations in Asp.Net MVC to Validate Model Data with Example - Tutlane
Use Data Annotations in Asp.Net MVC to Validate Model Data with Example - Tutlane

Before we start, ensure you have the latest version of .NET and Visual Studio installed. We'll be creating a new Web API project and adding basic authentication using the Microsoft.AspNetCore.Authentication.Core package.

ASP.NET Web API Tutorials For Beginners and Professionals
ASP.NET Web API Tutorials For Beginners and Professionals

Setting Up the Project and Authentication

Start by creating a new Web API project in Visual Studio. Once done, install the Microsoft.AspNetCore.Authentication.Core package via NuGet package manager.

ASP.Net Projects with Source Code
ASP.Net Projects with Source Code

Next, configure the authentication settings in the Startup.cs file under the ConfigureServices(IServiceCollection services) method. Here's how:

Configuring Basic Authentication

a web api with asp net core, net 6 0 build a web api with asp net core
a web api with asp net core, net 6 0 build a web api with asp net core

Add the following lines within the method to enable authentication and set up the authentication scheme:

services.AddAuthentication("BasicAuthentication").AddScheme("BasicAuthentication", options =>
{
    // Configure your options here.
});

Now, your services have been configured to use basic authentication. Next, you need to add it as a middleware in the Configure(IApplicationBuilder app, IWebHostEnvironment env) method.

Adding the Authentication Middleware

the back cover of an application with instructions for testing and troublesing it, including text
the back cover of an application with instructions for testing and troublesing it, including text

Add the following line within the method to add the authentication middleware:

app.UseAuthentication();

Now, your authentication middleware is set up and ready to use. Let's test the authentication in our Web API.

Protecting Web API Controllers

Ever needed to expose an endpoint to an external system but didn’t want to set up a full identity system? | Milan Jovanović
Ever needed to expose an endpoint to an external system but didn’t want to set up a full identity system? | Milan Jovanović

Now that we've set up the authentication let's protect our Web API controllers. To do this, we'll add a simple action filter and decorate our controllers with the [Authorize] attribute.

Creating the Action Filter

How to use Master Page in Asp.net
How to use Master Page in Asp.net
what is api? - screenshote for application programming and web development in the usa
what is api? - screenshote for application programming and web development in the usa
Part 4.  Range Validator in ASP NET Web Forms | ProgrammingGeek
Part 4. Range Validator in ASP NET Web Forms | ProgrammingGeek
Implementing Web APIs: Connect & Fetch Data Like a Pro 🌍🚀
Implementing Web APIs: Connect & Fetch Data Like a Pro 🌍🚀
owasp top 10 web application vulnerabilities
owasp top 10 web application vulnerabilities
Build a Very Cheap ASP.Net 5 Core Web Server With BeagleBone AI (BBAI)
Build a Very Cheap ASP.Net 5 Core Web Server With BeagleBone AI (BBAI)
Advanced Architecture for ASP.NET Core Web API
Advanced Architecture for ASP.NET Core Web API
Complete Beginner CSS Master Notes
Complete Beginner CSS Master Notes
How to call Stored Procedures in ASP.NET Core
How to call Stored Procedures in ASP.NET Core

Create a new class named BasicAuthenticationAttribute. This class should inherit from AuthorizationFilterAttribute and override the OnAuthorization method. Here's how:

Decorating Controllers with [Authorize]

Now, decorate your controllers with the [Authorize] attribute to protect them. For example:

The final step is to run your application and attempt to access your Web API. You should now be prompted with a basic authentication dialog, asking for your username and password.

With that, you've successfully implemented basic authentication in an Asp.NET Web API application. This hands-on example demonstrates how to set up and configure authentication, protect controllers, and test basic authentication. Now, dive deeper into other authentication options and securing your APIs.