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.
CodePudding user response:
You can use own implementation of IAuthorizationMiddlewareResultHandler
and register it as a service.
Take a look on this article: https://benfoster.io/blog/customize-authorization-response-aspnet-core/
CodePudding user response:
you can use the Unauthorized
method like below to return.
return Unauthorized("401 UnAuthorized");
CodePudding user response:
I think what you need is a custom implementation of the attribute as explained in this post: