Search code examples
asp.net-coreasp.net-web-apiblazor-server-sideblazor-webassemblyasp.net-blazor

Unable to Catch Unauthorized exception


In the webservice the following Post call is made.

try{
User = await _service.Post<User>("api/authenticate", model);
}
catch(Exception ex){
Console.Writeline($"exception{ex.Message}");
}

In the Controller if the user name or password is wrong it results in a unauthorized.

return Unauthorized(new{message = "PW wrong"}) 

or the user object

return Ok(user);

The service is able to catch a

return BadRequest();

response but not the Unauthorized response.

I was wondering if their is a difference between those responses.

I tried already to use System.Net.WebException like in this answer on StackOverFlow question

Thanks in advance


Solution

  • throw a Custom JSON HTTP response error and catch it