I have implemented a user validation class and a Basic Authentication Attribute class that verifies the token present in the header of a request by decoding and converting it. Next, the validation class checks the provided username and password. A GET request fails to prevent access to the data even if no username or password is provided.
I tried adding "[BasicAuthentication]" to a controller.
namespace minimalAPI.Controllers
{
[Route("v1/[controller]")]
[ApiController]
[BasicAuthentication]
I've also tried to make the authentication available to the entire application but didn't work either.
namespace minimalAPI
{
public class WebApiConfig
{
public void Register(HttpConfiguration config)
{
config.Filters.Add(new BasicAuthenticationAttribute());
}
}
}
Try adding [Authorize]
attribute to the controller. Here is good example to implement basic authentication in .NET 6 Web API