Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close
    Cheat sheets: all cheat sheets

    OAuth, OIDC, JWT and Cookie Security: A .NET Interview Decision Sheet

    Choose identity flows for the client and trust boundary.

    oauth-oidc-jwt-cookie-security-dotnet-interview-decision-sheet.pdf3 pages · 138 KB
    Every page, in this page.

    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?

    ClientFlowWhy
    Server-rendered web appAuthorization code (with PKCE), then a session cookieThe server keeps the tokens; the browser holds only a cookie
    Single-page appAuthorization 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 appAuthorization code with PKCE in the system browserA public client cannot keep a secret
    Service calling a serviceClient credentialsNo user is involved; the service signs in as itself
    NeverImplicit grant, password (ROPC) grantTokens in the redirect; the app handles the user's password

    Authorization code with PKCE, step by step

    1. The client makes a random code_verifier and sends its hash, the code_challenge (method S256), to the authorization endpoint, with a state value.
    2. The user signs in at the identity provider, never in the client.
    3. 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.
    4. The client sends the code and the code_verifier to the token endpoint. The provider checks that the verifier matches the challenge.
    5. The client calls the API with the access token issued for that API.
    ValueWhat it protects
    stateThe 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

    CheckWhat it means
    SignatureValid 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 typeAn 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:

    C#
    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

    ControlWhat it doesWhat it does not do
    HttpOnly cookieHides the cookie from page JavaScriptStop a script in the page from sending requests with it
    Secure cookieSends the cookie over HTTPS onlyDecide who may use it
    SameSite cookieHolds the cookie back from some cross-site requestsReplace an antiforgery token in every case
    Antiforgery tokenProves a form post came from your own pageMatter for an API that accepts only bearer tokens in a header: the browser never attaches those by itself
    CORSLets chosen origins read responses in a browserProtect 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.
    • AllowAnyOrigin together with AllowCredentials is 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 calls IAuthorizationService after loading:
    C#
    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 Authorization header.
    • 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?

    A Authorization code with PKCE
    B Implicit grant
    C Client credentials
    D Resource owner password (ROPC)
    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?

    A Accept it, because the signature is valid
    B Reject it: the audience is another API
    C Accept it if the issuer is trusted
    D Accept it if it has not expired
    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?

    A A stolen code from being redeemed
    B The access token from being read
    C The refresh token from being reused
    D The ID token from being replayed
    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?

    A It needs CORS to be secure
    B It must check antiforgery tokens
    C It is not exposed to CSRF through cookies
    D It no longer needs authorization checks
    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

    Practice

    TechInterviewNotes Editorial Team · Updated 28 Nov 2026 ·