WEB API 2.2: ApiController Unauthorized Method
asp.net, asp.net-web-api2
Solution
If your controller action method return `IHttpActionResult` then you can use this method as return type.
return Unauthorized();
You can also pass `AuthenticationHeaderValue` as parameter of this method which represents authentication information in Authorization, ProxyAuthorization, WWW-Authneticate, and Proxy-Authenticate header values.
If your action method does not return `IHttpActionResult` then you can throw `HttpResponseException` in anywhere from your controller action.
throw new HttpResponseException(HttpStatusCode.Unauthorized);
If you want to pass a custom message then use
var msg = new HttpResponseMessage(HttpStatusCode.Unauthorized)
{
ReasonPhrase = "Your message!"
};
throw new HttpResponseException(msg);
Problem
I'm working on a ASP.NET web application which contains both MVC and WEB API. Can anyone give me an example of how to use ApiController.Unauthorized Method in Web API. I am not sure what kind of parameter I should pass into this method.