从WebAPI返回401未经授权

Mat*_*att 0 asp.net-mvc asp.net-web-api

在我的WebAPI中,如果检测到错误授权,则使用以下内容:

   [HttpPost]
   public HttpResponseMessage CustomerQuotationComplete(Guid apiKey, int EnquiryId, int SiteId, bool accepted)
   {
      if (IsRequestAuthorized())
      {
        ...
      }
      else
      {
          var msg = new HttpResponseMessage(HttpStatusCode.Unauthorized) { ReasonPhrase = "Bad Authentication" };
          throw new HttpResponseException(msg);
      }
    }
Run Code Online (Sandbox Code Playgroud)

但是,我实际上收到的是302 Found响应,而不是401 Unauthorized。

那我在做什么错?

Ale*_*rt. 5

您可以返回未经授权的回复

[HttpPost]
   public IHttpActionResult CustomerQuotationComplete(Guid apiKey, int EnquiryId, int SiteId, bool accepted)
   {
      if (IsRequestAuthorized())
      {
        ...
      }
      else
      {
          return this.Unauthorized();
      }
    }
Run Code Online (Sandbox Code Playgroud)