gpt4 book ai didi

c# - 如何返回更具体的 HTTP 代码(如 401.X)

转载 作者:IT王子 更新时间:2023-10-29 04:46:52 26 4
gpt4 key购买 nike

Here是一篇关于一些扩展 HTTP 代码的文章:

IIS 7.0, IIS 7.5, and IIS 8.0 define several HTTP status codes that indicate a more specific cause of a 401 error. The following specific HTTP status codes are displayed in the client browser but are not displayed in the IIS log:

  • 401.1 - Logon failed.
  • 401.2 - Logon failed due to server configuration.
  • 401.3 - Unauthorized due to ACL on resource.
  • 401.4 - Authorization failed by filter.
  • 401.5 - Authorization failed by ISAPI/CGI application.

我需要一些像这样的自定义扩展 HTTP 代码:

  • 401.10 - User not found
  • 401.11 - User password mismatch
  • 401.12 - User account is locked
  • 401.13 - User account is expired
  • ...

如何从 .NET MVC 应用程序返回这些扩展的 401 错误代码?

最佳答案

在您的操作方法中,您手动设置状态代码:

Response.StatusCode = 401;
Response.SubStatusCode = 10;

此外,您可以在 System.Net 中使用 HttpStatusCode 枚举:

Response.StatusCode = (int)HttpStatusCode.Unauthorized;

关于c# - 如何返回更具体的 HTTP 代码(如 401.X),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25366985/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com