state
parameter that allows you to restore the previous state of your application. The state
parameter preserves some state objects set by the client in the Authorization request and makes it available to the client in the response.
state
parameter is to mitigate CSRF attacks by using a unique and non-guessable value associated with each authentication request about to be initiated. That value allows you to prevent the attack by confirming that the value coming from the response matches the one you sent.
The state
parameter is a string so you can encode any other information in it. You send a random value when starting an authentication request and validate the received value when processing the response. You store something on the client application side (in cookies, session, or localstorage) that allows you to perform the validation. If you receive a response with a state that doesn’t match, you can infer that you may be the target of an attack because this is either a response for an unsolicited request or someone trying to forge the response.
A CSRF attack specifically targets state-changing requests to initiate an action instead of getting user data because the attacker has no way to see the response to the forged request. For the most basic cases the state parameter should be a , used to correlate the request with the response received from the authentication.
Most modern OIDC and SDKs, including Auth0.js in single-page applications, handle the state generation and validation automatically.
414 Request-URI Too Large
, try a smaller value.
state
parameter to the request (URL-encoding if necessary). For example:
state
value will be included in this redirect. Note that depending on the type of connection used, this value might be in the body of the request or in the query string.
state
value and compare it with the one you stored earlier. If the values match, then approve the authentication response, else deny it.
state
parameter to encode an application state that will put the user where they were before the authentication process started. For example, if a user intends to access a protected page in your application, and that action triggers the request to authenticate, you can store that URL to redirect the user back to their intended page after the authentication finishes.
Generate and store a nonce locally (in cookies, session, or local storage) along with any desired state data like the redirect URL. Use the nonce as a state in the protocol message. If the returned state matches the stored nonce, accept the OAuth2 message and fetch the corresponding state data from storage. This is the approach we use in auth0.js.
redirectUrl
).
App Type | Storage Recommendation |
---|---|
Regular Web App | Cookie or session |
SPA | Local browser |
Native App | Memory or local |
redirect_uri
as well.
414 Request-URI Too Large
, try a smaller value.