OAuth, OIDC, JWT and Cookie Security: A .NET Interview Decision Sheet
Choose identity flows for the client and trust boundary.
Which sign-in flow fits which client, what an API must check in a token, and how cookies, CSRF and CORS fit together.
At a glance
- OAuth 2.0 lets a client get access to an API; OpenID Connect (OIDC) adds sign-in and an ID token on top of it.
- Interactive clients (web apps, single-page apps, mobile and desktop) use the authorization code flow with PKCE. Services use client credentials.
- Do not use the implicit grant or the password (ROPC) grant.
- An API accepts only access tokens issued for it: it checks the signature, issuer, audience and lifetime. An ID token is never an access token.
- A signed JWT that is not also encrypted is readable by anyone who holds it: the signature proves it was not changed, not that it is secret.
- Authentication says who is calling. Authorization decides what they may do to this resource. A valid token does not answer the second question.
Which flow for which client?
| Client | Flow | Why |
|---|---|---|
| Server-rendered web app | Authorization code (with PKCE), then a session cookie | The server keeps the tokens; the browser holds only a cookie |
| Single-page app | Authorization code with PKCE, or a backend for frontend (BFF) | No secret can be kept in the browser; a BFF keeps tokens on the server |
| Mobile or desktop app | Authorization code with PKCE in the system browser | A public client cannot keep a secret |
| Service calling a service | Client credentials | No user is involved; the service signs in as itself |
| Never | Implicit grant, password (ROPC) grant | Tokens in the redirect; the app handles the user's password |
Authorization code with PKCE, step by step
- The client makes a random
code_verifierand sends its hash, thecode_challenge(methodS256), to the authorization endpoint, with astatevalue. - The user signs in at the identity provider, never in the client.
- The provider redirects back to an exactly registered redirect URI (only a native app's localhost redirect may vary its port) with a short-lived code and the same
state. - The client sends the code and the
code_verifierto the token endpoint. The provider checks that the verifier matches the challenge. - The client calls the API with the access token issued for that API.
| Value | What it protects |
|---|---|
state | The redirect: it must belong to a sign-in this browser started (stops CSRF on the callback) |
PKCE (code_verifier) | The code: a stolen code is useless without the verifier |
nonce (OIDC) | The ID token: it must belong to this sign-in request (stops replay) |
What an API checks in a JWT access token
| Check | What it means |
|---|---|
| Signature | Valid under a signing key trusted for that issuer; unsigned tokens are refused. Pin the algorithms (ValidAlgorithms) when you know the issuer's |
Issuer (iss) | The exact issuer you trust |
Audience (aud) | This API. A token for another API, or an ID token, is rejected |
Lifetime (exp, nbf) | Not expired and already valid, with a small clock skew (5 minutes by default) |
| Token type | An access token, not an ID token. The audience check stops most mix-ups; to check the type itself, set ValidTypes (for example at+jwt) |
In ASP.NET Core, the JWT bearer handler checks the signature, issuer, audience and lifetime, with keys from the authority's metadata. Set the authority and the audience, and keep the defaults:
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.Authority = "https://login.example.com/";
options.Audience = "orders-api";
});
builder.Services.AddAuthorizationBuilder()
.AddPolicy("CanWriteOrders", p => p.RequireClaim("permission", "orders.write"));Cookies, CSRF and CORS
| Control | What it does | What it does not do |
|---|---|---|
HttpOnly cookie | Hides the cookie from page JavaScript | Stop a script in the page from sending requests with it |
Secure cookie | Sends the cookie over HTTPS only | Decide who may use it |
SameSite cookie | Holds the cookie back from some cross-site requests | Replace an antiforgery token in every case |
| Antiforgery token | Proves a form post came from your own page | Matter for an API that accepts only bearer tokens in a header: the browser never attaches those by itself |
| CORS | Lets chosen origins read responses in a browser | Protect anything: it relaxes the browser's rule; non-browser clients ignore it |
- CSRF needs a credential the browser sends by itself, such as a cookie or Basic authentication. An API that accepts only a bearer token in a header is not exposed to it.
AllowAnyOrigintogether withAllowCredentialsis an insecure CORS setup: list the allowed origins.- Never change data on a GET request.
Authorization after authentication
- An endpoint policy answers "may this caller use this operation?". A resource check answers "may this caller change order 124?".
- Load the resource inside the caller's tenant, then check it. Hard-to-guess IDs do not replace the check (IDOR).
- In ASP.NET Core,
[Authorize]runs before the resource is loaded, so a resource check callsIAuthorizationServiceafter loading:
var result = await authorizationService.AuthorizeAsync(User, order, "SameTenant"); if (!result.Succeeded) return Forbid();
Signing out and revoking
- Without an extra check, a self-contained access token stays usable until it expires: an API that validates it locally does not learn that the user signed out.
- Keep access tokens short-lived, revoke the refresh token at sign-out, and add a revocation check only where the risk needs it.
- Refresh tokens of public clients must be sender-constrained or rotated (each use returns a new one, and a reused old one is refused).
Common mistakes
- Sending an ID token to an API: request an access token for that API instead.
- Decoding a JWT and trusting its claims without validating it: let the bearer handler validate it.
- Putting an access token in a URL query string: send it in the
Authorizationheader. - Accepting any redirect URI that starts with a known prefix: register and match redirect URIs exactly.
- Treating CORS as API security: authenticate and authorize every request.
- Checking only a role for a resource action: also check that the resource belongs to the caller's tenant.
In an interview
Cookie or JWT for a server-rendered web app? A secure, HttpOnly cookie session. Tokens are for calling APIs; the browser does not need to hold them.
Why PKCE if you already send state? state ties the callback to the sign-in this browser started. PKCE ties the code to the client that asked for it, so a stolen code cannot be redeemed; with a server that supports PKCE, it also stops CSRF on the callback.
Can an API accept an ID token? No. An ID token tells the client who signed in. An API needs an access token whose audience is that API.
Is a JWT encrypted? A signed JWT (JWS) is not: anyone holding it can read its claims. The signature proves it was not changed and was signed with the issuer's key. Only an encrypted JWT (JWE) hides the claims.
How do you revoke a JWT access token? An API that validates it locally accepts it until it expires. Keep lifetimes short, revoke the refresh token, and add a revocation check where the risk needs it.
Does CORS protect an API? No. CORS only controls what browser pages from other origins may read. Every request still needs authentication and authorization.
What is the difference between authentication and authorization? Authentication proves who the caller is. Authorization decides whether that caller may do this action on this resource.
Check yourself
Which flow should a single-page app use to sign users in?
Show answer
Correct answer: A
A single-page app is a public client: it uses the authorization code flow, with PKCE protecting the code.
Option B: The implicit grant returns tokens in the redirect and is no longer recommended.
Option C: Client credentials is for a service acting as itself, with no user.
Option D: ROPC hands the user's password to the app and does not work with multifactor sign-in.
An orders API receives a valid, signed token whose audience is the inventory API. What should it do?
Show answer
Correct answer: B
A token is only for the audience it names. The orders API must reject a token issued for another API.
Option A: A valid signature proves who issued the token, not who it is for.
Option C: A trusted issuer also issues tokens for other APIs.
Option D: Lifetime is one check among several; the audience still fails.
What does PKCE protect?
Show answer
Correct answer: A
Only the client that made the code_verifier can exchange the code for tokens.
Option B: PKCE does not encrypt anything; a signed JWT stays readable.
Option C: Refresh tokens are protected by rotation or sender-constraining, not by PKCE.
Option D: The nonce protects the ID token from replay.
An API is called only with bearer tokens in the Authorization header. Which statement is true?
Show answer
Correct answer: C
CSRF works because the browser attaches cookies by itself. A header token is never attached automatically.
Option A: CORS controls what browsers may read; it adds no protection.
Option B: Antiforgery tokens protect cookie-based forms, not header tokens.
Option D: Every request still needs authorization, whatever carries the token.
Sources
- Authorization code flow (Microsoft identity platform): use the code flow with PKCE for single-page apps.
- Implicit grant flow and ROPC flow: Microsoft recommends using neither.
- Configure JWT bearer authentication in ASP.NET Core: the checks an API makes; ID tokens never call APIs.
- Enable CORS in ASP.NET Core: CORS is not a security feature; AllowAnyOrigin with AllowCredentials is insecure.
- Resource-based authorization in ASP.NET Core: attributes run before the resource loads; IAuthorizationService.AuthorizeAsync.
- RFC 9700, OAuth 2.0 Security Best Current Practice: PKCE for public clients, exact redirect matching, no tokens in query strings, refresh token rotation.
Practice
TechInterviewNotes Editorial Team · Updated 28 Nov 2026 ·