HTTP defines the status 401 Unauthorized for missing authentication, but this status only applies to HTTP authentication. What status should I return with a session cookie based system, when an unauthorized request happens?
-
This question is similar to: 403 Forbidden vs 401 Unauthorized HTTP responses. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem.TylerH– TylerH2025-04-04 13:54:12 +00:00Commented Apr 4 at 13:54
2 Answers
Formally, 403 Forbidden is the right response. It's defined as
Authorization will not help and the request SHOULD NOT be repeated.
The confusing part may be "Authorization will not help", but they really mean "HTTP authentication" (WWW-Authenticate)
5 Comments
403 I believe is technically correct (and probably most effective if you are implementing a custom API / protocol).
401 is not appropriate as it refers to authorization with a WWW-Authenticate header, which a session cookie is not.
If this is a public facing website where you are trying to deny access based on a session cookie, 200 with an appropriate body to indicate that log in is needed or a 302 temporary redirect to a log in page is often best.