Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return more specific HTTP code (like 401.X)

Tags:

c#

asp.net-mvc

Here is an article about some extended HTTP codes:

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.

I need some custom extended HTTP codes like these:

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

How to return these extended 401 error codes from .NET MVC application?

like image 999
wishmaster Avatar asked Aug 18 '14 15:08

wishmaster


People also ask

What is difference between 401 and 403 HTTP status?

401 Unauthorized is the status code to return when the client provides no credentials or invalid credentials. 403 Forbidden is the status code to return when a client has valid credentials but not enough privileges to perform an action on a resource.

What is a 402 error code?

The HTTP 402 Payment Required is a nonstandard response status code that is reserved for future use. This status code was created to enable digital cash or (micro) payment systems and would indicate that the requested content is not available until the client makes a payment.

What does the HTTP status code 401 indicates?

The HyperText Transfer Protocol (HTTP) 401 Unauthorized response status code indicates that the client request has not been completed because it lacks valid authentication credentials for the requested resource.


1 Answers

In your action method you set the status codes manually:

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

Additionally, you can use the HttpStatusCode enumeration in System.Net:

Response.StatusCode = (int)HttpStatusCode.Unauthorized;
like image 71
DavidG Avatar answered Sep 22 '22 15:09

DavidG