WEB API 2.2: Unauthorized ApiController Method
1 answer
If your controller action method returns IHttpActionResult
, you can use that method as the return type.
return Unauthorized();
You can also pass AuthenticationHeaderValue
as a parameter to this method, which represents authentication information in the Authorization, ProxyAuthorization, WWW-Authneticate, and Proxy-Authenticate header values.
If your action method doesn't return IHttpActionResult
, you can throw HttpResponseException
anywhere from your controller action.
throw new HttpResponseException(HttpStatusCode.Unauthorized);
If you want to pass a custom message use
var msg = new HttpResponseMessage(HttpStatusCode.Unauthorized)
{
ReasonPhrase = "Your message!"
};
throw new HttpResponseException(msg);
+3
source to share