Reputation: 6766
I have an ActionFilterAttribute
in which I am checking for a valid license. I would like to return a 401 error along with a message. Right now, I have the following implementation:
public override void OnActionExecuting(ActionExecutingContext context) {
...
// return errror (short-circuit)
context.Result = new StatusCodeResult(401);
return;
}
How can I also pass a message? Inside my controllers, I would do the following:
return Unauthorized("Some error message");
Upvotes: 6
Views: 4300
Reputation: 93063
The ASP.NET Core source code is available on GitHub, where it shows that the implementation for the Unauthorized
method used in a controller looks like this (source):
public virtual UnauthorizedObjectResult Unauthorized([ActionResultObjectValue] object value)
=> new UnauthorizedObjectResult(value);
You can emulate this inside of your OnActionExecuting
implementation, like this:
context.Result = new UnauthorizedObjectResult("Some error message");
Upvotes: 6