Search code examples
c#asp.net-web-apiswagger.net-6.0basic-authentication

How do I make basic authentication available to my controller in asp.net web api .net 6?


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());
        }
    }
}


Solution

  • Try adding [Authorize] attribute to the controller. Here is good example to implement basic authentication in .NET 6 Web API