ASP.NET Web API : Correct way to return a 401/unauthorised response

后端 未结 8 1189
后悔当初
后悔当初 2020-11-28 08:41

I have an MVC webapi site that uses OAuth/token authentication to authenticate requests. All the relevant controllers have the right attributes, and authentication is workin

相关标签:
8条回答
  • 2020-11-28 09:40

    you can use follow code in asp.net core 2.0:

    public IActionResult index()
    {
         return new ContentResult() { Content = "My error message", StatusCode = (int)HttpStatusCode.Unauthorized };
    }
    
    0 讨论(0)
  • 2020-11-28 09:45

    You also follow this code:

    var response = new HttpResponseMessage(HttpStatusCode.NotFound)
    {
          Content = new StringContent("Users doesn't exist", System.Text.Encoding.UTF8, "text/plain"),
          StatusCode = HttpStatusCode.NotFound
     }
     throw new HttpResponseException(response);
    
    0 讨论(0)
提交回复
热议问题