0Pricing
OAuth2 & OpenID Connect Deep Dive · บทเรียน

การใช้ nonce เพื่อป้องกันการเล่นซ้ำ

เรียนรู้วิธีที่พารามิเตอร์ nonce ของ OpenID Connect ผูกโทเค็น ID เข้ากับคำขอยืนยันตัวตนเฉพาะรายการ และป้องกันการโจมตีด้วยการเล่นโทเค็นซ้ำ

การใช้ nonce เพื่อป้องกันการเล่นซ้ำ เป็นบทเรียน OAuth2 & OpenID Connect Deep Dive ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน OAuth2 & OpenID Connect Deep Dive และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส OAuth2 & OpenID Connect Deep Dive มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

What Is the nonce?

The nonce is a random value the client generates and includes in the authentication request. The OpenID Provider echoes it back inside the issued ID token. Matching them proves the token belongs to this request.

The Replay Threat

Without a nonce, an attacker who captures a valid ID token (for example, in the Implicit or Hybrid flow where tokens travel via the browser) could replay it into another session. The nonce ties the token to one specific request, defeating replay.

nonce vs state

They are different tools:

  • state — protects the OAuth2 authorization request/response against CSRF.
  • nonce — protects the ID token against replay, validated inside the token itself.

Use both together in OIDC flows.

Generating a nonce

Create a high-entropy random value and store it bound to the user's session before redirecting.

import secrets
nonce = secrets.token_urlsafe(32)
session['oidc_nonce'] = nonce
print(nonce)

Including It in the Request

Add the nonce to the authorization request alongside the usual parameters.

GET /authorize?
  response_type=code
  &client_id=app123
  &scope=openid profile
  &redirect_uri=https://app.example.com/cb
  &state=xyz
  &nonce=Tk9SQ0VfdmFsdWU

It Comes Back in the ID Token

The ID token's payload includes the exact nonce you sent.

{
  "iss": "https://op.example.com",
  "sub": "248289",
  "aud": "app123",
  "nonce": "Tk9SQ0VfdmFsdWU",
  "exp": 1735689600
}

Validating the nonce

After validating the ID token's signature and claims, compare its nonce with the value stored in the session.

if id_token['nonce'] != session.pop('oidc_nonce', None):
    raise Exception('nonce mismatch - reject token')

When nonce Is Required

The nonce is mandatory in the Implicit and Hybrid flows because ID tokens are returned through the browser front channel. In the Authorization Code flow it is recommended and strongly encouraged.

One-Time Use

Treat each nonce as single-use. Remove it from the session as soon as it is validated so the same value can never authorize a second token, closing replay windows.

Common Mistakes

Pitfalls to avoid:

  • Using a predictable or reused nonce.
  • Forgetting to compare it after validating the signature.
  • Storing it client-side without integrity protection.
  • Skipping it in front-channel flows.

Putting It Together

The full lifecycle: generate nonce, store in session, send in auth request, receive it in the ID token, verify signature and claims, then compare and discard the nonce. Only then trust the authentication.

Quick Check

Test your knowledge of the nonce.

Recap

The nonce protects ID tokens from replay.

  • Generate a random nonce, store it in session, send it in the auth request.
  • The OP echoes it inside the ID token.
  • Validate by comparing token nonce to session nonce, then discard it.
  • Required in Implicit/Hybrid flows; recommended everywhere.

คำถามที่พบบ่อย

บทเรียน “การใช้ nonce เพื่อป้องกันการเล่นซ้ำ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การใช้ nonce เพื่อป้องกันการเล่นซ้ำ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส OAuth2 & OpenID Connect Deep Dive ให้อัปเกรดเป็น CoddyKit PRO คอร์ส OAuth2 & OpenID Connect Deep Dive มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การใช้ nonce เพื่อป้องกันการเล่นซ้ำ”

เรียนรู้วิธีที่พารามิเตอร์ nonce ของ OpenID Connect ผูกโทเค็น ID เข้ากับคำขอยืนยันตัวตนเฉพาะรายการ และป้องกันการโจมตีด้วยการเล่นโทเค็นซ้ำ คุณปฏิบัติ OAuth2 & OpenID Connect Deep Dive ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน OAuth2 & OpenID Connect Deep Dive หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน OAuth2 & OpenID Connect Deep Dive บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การใช้ nonce เพื่อป้องกันการเล่นซ้ำ” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน OAuth2 & OpenID Connect Deep Dive นี้ได้ไหม

ได้ บทเรียน OAuth2 & OpenID Connect Deep Dive ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. โฟลว์รหัสอนุญาตสิทธิ์ด้วย OIDC
  2. โฟลว์แบบแฝงด้วย OIDC
  3. โฟลว์แบบผสมด้วย OIDC
  4. การใช้ nonce เพื่อป้องกันการเล่นซ้ำ
← กลับไปที่ OAuth2 & OpenID Connect Deep Dive