Sessions, Cookies, OAuth, and OpenID Connect

After sign-in the app needs to recognize later requests. A first-party app can use a server-side session. An app working with an identity provider can also receive identity information and delegated access tokens.

On this page

Issue and revoke a server-side session

Use a framework-generated unpredictable identifier in a host-only cookie. Secure restricts transmission to HTTPS, HttpOnly prevents JavaScript from reading the cookie, and SameSite limits cross-site sending. The __Host- prefix requires Secure, Path=/, and no Domain. Rotate the ID on login or privilege changes. Enforce idle and absolute expiry on the server and revoke it at logout.

Set-Cookie: __Host-session=<opaque-random-id>; Path=/; Secure; HttpOnly; SameSite=Lax

Choose the right token for the recipient

OAuth delegates access to an API. OpenID Connect adds sign-in and an ID token for the client. Use a maintained library for authorization code flow with PKCE and exact redirect URIs. Validate tokens for their intended use. For signed JWT access tokens the API checks the expected algorithm, signature, issuer, audience, expiry, and permissions. Signed claims can be read, so keep secrets out.

ID token       Client verifies sign-in for this client
Access token   Resource server verifies access for this API
Refresh token  Client uses the token endpoint to renew access

Official references