1

Here is my controller code. my controller return 401 status code successfully.

How to return a 401 error message with my custom message

[HttpGet]
        [Authorize(Roles = "reader")]
        public async Task<IActionResult> GetAllBlogsAsync()
        {
            // get data from repository
            var blogs = await blogRepository.GetAllAsync();

            // mapping Domain to DTO
            var blogsDTO = mapper.Map<List<Models.DTO.Blog>>(blogs);

            // return results
            return Ok(blogsDTO);
        }

My expected Output is "401 UnAuthorized"

Advance Thanks.

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197

2 Answers2

0

you can use the Unauthorized method like below to return.

return Unauthorized("401 UnAuthorized");
Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
0

I think what you need is a custom implementation of the attribute as explained in this post:

How to return custom message if Authorize fails in WebAPI

WerkmanW
  • 86
  • 7