'how to send/create status code as response for Unauthorized requests in .Net Framework 4.0
I am using .NET framework 4.0 with WEB API 1.0. I am trying to send an HTTP 401 Unauthorized response for an unauthorized API request, but every time I receive a status code 200 OK. In the response body I get:
StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.StringContent, Headers:
{
Content-Type: text/plain; charset=utf-8
}
Below is my code:
[HttpPost]
public HttpResponseMessage GetShopredact()
{
var response = new HttpResponseMessage(HttpStatusCode.Unauthorized)
{
Content = new StringContent("Invalid Request", System.Text.Encoding.UTF8, "text/plain"),
StatusCode = HttpStatusCode.Unauthorized
};
return response;
}
I am unable to use the System.Web.Http
namespace because of the framework restriction.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|