I am creating a security controller within my asp.net core API. My Blazor Web app connects to the ASP.NET Core API and it communicates to the Auth0 Management API. I have setup Auth0 / asp.net core API to utilize M2M.
Everything works as expected (i.e. tokens, rights, etc) BUT for some reason I am not able to catch the status code from Auth0 (i.e. Auth0 is not throwing the error as I would have expected. Is there anotherWhen I pass a valid auth0id, I get the json response (perfect). When I pass an invalid ID, I know that Autho throws a 404 error (The user does not exist). I am wanting to capture Auth0 exception so that I can handle it using Status Codes instead of description.
The exception handler I have been able to find is ErrorApiException. If this is not the correct one, what should I be using? Auth0 is throwing an expection because I can able to handle viw catch (Exception ex) when (ex.Message == "The user does not exist.").
using Auth0.Core.Exceptions;
using Auth0.ManagementApi; (version I am using is 10.0.0)
...
public class SecurityController (IManagementApiClient managementApiClient, ILogger<ItemsController> logger): ControllerBase
{
private readonly IManagementApiClient _managementApiClient = managementApiClient;
....
[HttpGet("User/{auth0id}")]
[Authorize(Policy = "Read:Security_Tables")]
public async Task<ActionResult<UserModel>> GetUser(string auth0id)
{
_logger.LogInformation("{UserId} - Get: api/Security/User/{auth0id}", User.FindFirst(ClaimTypes.NameIdentifier)!.Value, auth0id);
try
{
GetUserResponseContent result = await _managementApiClient.Users.GetAsync(
id: auth0id, request: new GetUserRequestParameters
{
IncludeFields = true
});
UserModel user = new UserModel
{
User_Id = result.UserId,
Email = result.Email!,
Email_Verified = (result.EmailVerified == true ? true : false),
Username = result.Username,
Phone_Number = result.PhoneNumber,
Name = result.Name!,
Nickname = result.Nickname,
Given_Name = result.GivenName!,
Family_Name = result.FamilyName!,
Blocked = (result.Blocked == true ? true : false)
};
return Ok(user);
}
catch (ErrorApiException ex)
{
System.Net.HttpStatusCode statusCode = ex.StatusCode;
return NotFound(ex.Message);
}
catch (Exception ex) when (ex.Message == "The user does not exist.")
{
return NotFound(ex.Message);
}
catch (Exception ex)
{
return BadRequest();
}
}
Thanks everyone