SignalR client get 403 forbidden error when jwt cookie expire - asp.net

My SignalR app use JWT authentication cookie for authentication. For testing I set the jwt exp payload to 3 min.
I tested in Chrome browser, transport using serverSentEvent.
I have a button on page which will trigger Hub's server method.
After 3 min, when I click on the button. The Hub will response 403 forbidden error. I guess it is because my jwt token expired.
However if then I reload the page, the hub still connected and started. The page still use the same cookie sent to server. When I click on the button to trigger server's method. This time it will not return 403 error. The server method still get called. But inside the server method, the Context.User.Identity.IsAuthenticated = false.
I can't understand why on the first load will trigger 403 error, but not on the reloaded page.
I want to catch the unauthorize access to signalr, if the jwk token is expired or contain invalid data, I will redirect to login page.

Related

HTTP status if re-authentication is required

Which status code would you use in this scenario, assuming you're using a token based authentication:
The client has a token and makes a request to the server.
The token expired and the server sends a 401 Unauthorized.
The client sends the refresh token.
The token is invalid and the server responds with XXX?
The use case would be an application, that automatically catches 401's and makes a request with the refresh token. If the server would respond with a 401 if this token is not valid, the client would try to request a new access token with the refresh token forever. But it should tell the client, that it should re-authenticate with its credentials (e.g. email and password).
I was just wondering which status code would be the best fit in this scenario, as the spec says in case of a 403 Forbidden "authorization will not help".
I would not make access and refresh tokens interchangeable: Use Access-Tokens to access protected resources and use Refresh-Token to fetch new Access-Token from a special end-point. OpenID Connect works this way.
You would have one HTTP request more but HTTP codes would not be a problem and, in my opinion, you would get a cleaner code.

Asp.net mvc [Authorize] attribute will return http 302 instead of http 401 if the authorization fail

I have created a new asp.net mvc 5 web application and I use individual accounts (form authentication) authentication. this will build a full Account controller.
Then I created a new controller named People , and I add [Authorize] attribute before it as follow:-
[Authorize]
public class PeopleController : Controller
{
Then I try to directly access the controller without login, so I was redirected to the login page. But what I have noticed is that using IE F12 developer tool, the http status will be 302 and not 401 , although according to http standards if the request fail authentication then the application should return http 401, this what I get:-
Can anyone advice on this? I mean should any application be fully compliance with the http standards? Or there is no problem of redirecting users to login page if the user is not authenticated without raising an http 401 error (as the default asp.net mvc is doing)?
Thanks
Returning 302 or 401 http status code in this case depends on if you want to control what happens when the user is unauthorized from the server side or the client side. It also depends on whether the request you made was a full page submit or an AJAX request. AJAX style requests typically prefer 401 while full page submits prefer 302 redirection.
If you want handle the unauthorized case on the server side and redirect the user back to login page, then the common practice is to return a 302 redirection to that page.
If you want to handle this case on the client side, then you can have the request return 401. Then, you will have a 401 handler in the client which will detect it and do a client side redirect to the login page. 401 is also the way to do it if instead of redirection, you want to show a message on the same page that the user is no longer authorized.
You will see Web API Authorize attribute return 401 and MVC Authorize attribute do a 302 redirect.
302 is a pretty common way to do url redirection. It would be the url redirect that is causing the 302 rather than a 401 error. It indicates that it has moved you from /people/ back to /Account/Login in your case.

using http 401 Versus http 403 for unauthorized requests , which is more appropriate

I read a lot about using http 403 or http 401 for managing unauthorized requests.
I think that if my application uses one phase authentication then using http 403 is more appropriate. since the http 401 response definition indicates that authentication is required, so if a user is not loggin and he tries to access a page then the application should require a username/password. but if the user is authenticated and the application have only one phase authentication then returning 403 is the right code if the user is not authorized, since even if the user re-type his username and password ,, then nothing will chnage. but if my application requires two phase authentication ,for example to enter a second password then returning http 401 will be more appropriate. so can any one aivce ?

Handle OAuth2 authentication failure using Apigee proxy

I've written my own login app to protect my api following the oauth-login-app example.
I've implemented the web server flow and everything works great.
My question is: how should I handle an authentication failure at step 3? How do I tell he client app that the authentication failed? The user could either press the cancel button, or refuse permission or just enter the wrong details.
When you initiate OAuth 2.0 (dance) with
/authorize
the user-agent land on /login page (created/hosted by you),
post redirect.
enduser(user-agent) submits the username/password
to the page hosted by you. Here you collect the credentials and
submit to Apigee, and if authentication fails, send a HTTP 401
response. Now your application should be in position to re-render
the login page and with a flash "invalid credential".
Now coming to if user is authenticated but rejects the authorization request in
consent page, you should redirect to the "redirect_uri" provided
by client, with error code.
How do I tell he client app that the authentication failed?
The login app will redirect the control back to the application redirect URI - with added error code/description in the URL as hash parameters. In case of success the URL is appended with code or token.
You can do this redirect from your login app directly but I would suggest to make the redirect call first to an Apigee Proxy and let Apigee Proxy send the redirect back to app. Both in case of success and failure. In this way you will have the benefit of using Apigee analytics that helps your understand how many OAuths failed for what reason etc.
EDIT:
You can use the same GenerateAuthorizationCode proxy you have built for the success flow. When login fails or succeeds, in either case you need to pass that information to this proxy. Generally the login app and this proxy should share this information using a common session store. You can not pass this information just using a redirect parameter because that can be changed by the client user agent. When you redirect to the GenerateAuthorizationCode redirect proxy, do so by appending a random session ID in the URL. That id can be used by the GenerateAuthorizationCode proxy to look up the login status from the session store. Then you can either send back a redirect with error or a proper oauth code based on if the login was successful. An easy implementation of the session store can be done using a distributed caching resource in the apigee gateway. Login app can put/get the session using an internal API. While the proxy can use policies to retrieve the session information.

What is the recommend workflow for doing web authentification (HTTP status codes)?

I'm writing a web application which requires a user login. As you might recognise there are many workflows for responding on invalid user credentials. Most browsers are captable of storing (accepted) user credentails in their credential storage.
My original question splits up into two parts:
Which HTTP status code should be send to the user if a site requires a logged in user? I don't mean the situation the user is requesting the login page by herself.
Which HTTP status code should be send to the user if he/she has entered invalid credentials?
HTTP 401 is only for the "old style" authentification via browser prompts I guess. I'm using my own login pages to get rid of those browser prompts.
There are many standards for the authentication. Couple of example you can find here:
http://docs.oracle.com/javaee/1.4/tutorial/doc/Security5.html
The error code 401 you described is used for the Basic Authentication. It is not old :).
When you use your own login form please follow the standard Form authentication flow:
If a user access a protected resource without an authentication redirect to a login page (HTTP code 302)
Show the login page (HTTP code 200). Submit a user name to a dedicated URL (generally it is j_security_check)
After a successful authentication redirect back to the original protected resource (HTTP code 302)
After a failed authentication show once again to the login page with the error code (HTTP code 200)
If a user access a protected resource after the authentication show the resource (HTTP code 200)
To distinguish between an authenticated and not an authenticated session you can use HTTP cookie.
It is recommended to use GUID and not any user information (e.g. user name).
401 Unauthorized / 407 Proxy Authentication Required are the only status codes that are relevant.
401 Unauthorized should only be presented if you are denying access to any resource - NOT if you are redirecting or presenting them with a login page.
407 is just like 401 except you are expecting auth to be handled by an external service and you expect to be provided with a Proxy-Authenticate header field.
More typically, you are presenting the user with a 200 OK and a login page.

Resources