WEB API 2.2: Unauthorized ApiController Method

I am working on an ASP.NET web application that contains MVC and WEB API. Can anyone give me an example on how to use ApiController.Unauthorized Method in Web API. I'm not sure what parameter I should pass to this method.

+3


source to share


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







All Articles