title: How can I use basic authentication in my application?
body: <p>How can I use basic authentication in the ASP.NET Core API?</p>
  <p>I have the below ASP.NET web API controller. How can I use the middleware for authentication or any other method to achieve the basic authentication in the ASP.NET Core web API?</p>
  <pre><code>namespace Test.Web.Controllers
  {
      [Route(&quot;api/[controller]&quot;)]
      public class TestAPIController : Controller
      {
          // GET: api/&lt;controller&gt;
          [HttpGet]
          public IEnumerable&lt;string&gt; Get()
          {
              return new string[] { &quot;value1&quot;, &quot;value2&quot; };
          }
  
          // GET api/&lt;controller&gt;/5
          [HttpGet(&quot;{id}&quot;)]
          public string Get(int id)
          {
              return &quot;value&quot;;
          }
  
          // POST api/&lt;controller&gt;
          [HttpPost]
          public void Post([FromBody]string value)
          {
          }
  
          // PUT api/&lt;controller&gt;/5
          [HttpPut(&quot;{id}&quot;)]
          public void Put(int id, [FromBody]string value)
          {
          }
  
          // DELETE api/&lt;controller&gt;/5
          [HttpDelete(&quot;{id}&quot;)]
          public void De`enter code here`lete(int id)
          {
          }
  
      }
  }
  </code></pre>
  <p>I have seen the below middleware. How can I use the middleware in the controller?</p>
  <p>Do I need to configure any additional setting?</p>
  <pre><code>public class AuthenticationMiddleware
  {
      private readonly RequestDelegate _next;
  
      public AuthenticationMiddleware(RequestDelegate next)
      {
          _next = next;
      }
  
      public async Task Invoke(HttpContext context)
      {
          string authHeader = context.Request.Headers[&quot;Authorization&quot;];
          if (authHeader != null &amp;&amp; authHeader.StartsWith(&quot;Basic&quot;))
          {
              // Extract credentials
              string encodedUsernamePassword = authHeader.Substring(&quot;Basic &quot;.Length).Trim();
              Encoding encoding = Encoding.GetEncoding(&quot;iso-8859-1&quot;);
              string usernamePassword = encoding.GetString(Convert.FromBase64String(encodedUsernamePassword));
  
              int seperatorIndex = usernamePassword.IndexOf(':');
  
              var username = usernamePassword.Substring(0, seperatorIndex);
              var password = usernamePassword.Substring(seperatorIndex + 1);
  
              if(username == &quot;test&quot; &amp;&amp; password == &quot;test&quot; )
              {
                  await _next.Invoke(context);
              }
              else
              {
                  context.Response.StatusCode = 401; // Unauthorized
                  return;
              }
          }
          else
          {
              // No authorization header
              context.Response.StatusCode = 401; // Unauthorized
              return;
          }
      }
  }
  </code></pre>