I am trying to write a custom Authorize attribute to authorize some of the API endpoints and MVC actions. Following this StackOverflow answer, I wrote a custom attribute. I am using UnauthorizedResult to return 401.
- For Web API, How can I return status codes 401 or 403 along with some additional message as JSON payload?
- For MVC Actions that return HTML, How can I return status codes 401 or 403 and redirect to different URL?
- How can I check if the request is WebAPI or MVC action?