'statusText always shows OK for StatusCode 401 where as expecting "Unauthorized" for 401

I dont understand why do I always see statusText as OK for StatusCode 401, whereas expecting "Unauthorized" for 401. Below is my code snippit. Please help me to resolve this issue.

public class BuggyController : BaseApiController
{
    [Authorize]
    [HttpGet("auth")]
    public ActionResult<string> GetSecret()
    {
        return "secret text";
    }
}

//Anguler call to webApi
get401Error(){
this.http.get(this.baseUrl + 'buggy/auth')
.subscribe(response => {
  console.log(response);
}, error => {
  console.log(error);
})

}

Chrome response image



Solution 1:[1]

After some code review in detail, I found that you're also using UseHttpsRedirection middleware. Your Angular app is running in development mode and there is no SSL. Both the API and Client are running without any certificate. So please remove UseHttpsRedirection middleware, then correct HttpErrorResponse is received. IT is in Startup.cs Configure method.

Comment this out: app.UseHttpsRedirection();

Also, could you share your Startup class for more details. It could also happen if CORS is configured in the wrong way.

Solution 2:[2]

Cool, I had the same issue, but I figured it out:

  1. Comment this out: app.UseHttpsRedirection();
  2. Instead of making any request with https://localhost:5001/api, please change it to http://localhost:5000/api/

That was my solution.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 shayar shrestha
Solution 2 Pedro Manuel Salvador Ayala