Question 388 of 997
Implement Azure securityhardMultiple ChoiceObjective-mapped

Quick Answer

The correct answer is to use the OnTokenValidated event in OpenID Connect middleware to query the database and add custom role claims to the identity. This approach works because after Microsoft Entra ID validates the token, the OnTokenValidated event fires, giving you a hook to enrich the ClaimsPrincipal with dynamic role mappings from your application database before the request pipeline continues. On the AZ-204 exam, this tests your understanding of how to implement custom role authorization from database OnTokenValidated without modifying Entra ID configuration—a common scenario when roles are fluid and managed externally. A frequent trap is assuming you must define roles in Entra ID app roles or groups, but the exam expects you to know that OnTokenValidated allows runtime claim injection for flexible authorization. Memory tip: think “validate then hydrate”—validate the token, then hydrate the identity with database roles.

AZ-204 Implement Azure security Practice Question

This AZ-204 practice question tests your understanding of implement azure security. This is a configuration task: choose the command set that satisfies every stated requirement. Small differences — like 'secret' vs 'password' or 'transport input ssh' vs 'all' — change whether the answer is correct. After answering, compare your reasoning against the explanation and wrong-answer breakdown below. Once you have made your selection, read the full explanation to reinforce the concept and understand why each distractor is designed to mislead on exam day.

You are designing an ASP.NET Core web API that authenticates users via Microsoft Entra ID. The application needs to authorize access to resources based on custom roles (e.g., 'Admin', 'Editor') that are not defined in Microsoft Entra ID app roles or groups. The role mappings are dynamic and stored in an application database. How should you implement authorization in the API?

Question 1hardmultiple choice
Full question →

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

Use the OnTokenValidated event in OpenID Connect middleware to query the database and add custom role claims to the identity.

Option B is correct because it uses the OnTokenValidated event in OpenID Connect middleware to enrich the user's identity with custom role claims from the application database after token validation. This approach allows dynamic role mappings stored externally to be injected into the ClaimsPrincipal, which can then be evaluated by the standard [Authorize] attribute with role policies. It avoids modifying Entra ID configuration and keeps role management flexible within the application.

Key principle: Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • Define the roles as Microsoft Entra ID app roles and assign them to users. This is the standard way to handle roles.

    Why it's wrong here

    The scenario explicitly states that roles are not defined in Microsoft Entra ID and are dynamic. Relying on app roles would not work because the roles are not in the token.

  • Use the OnTokenValidated event in OpenID Connect middleware to query the database and add custom role claims to the identity.

    Why this is correct

    Correct. By subscribing to the OnTokenValidated event, you can retrieve roles from the database and add them as claims to the principal. Then you can use standard authorization policies based on those claims.

    Related concept

    Read the scenario before looking for a memorised answer.

  • Store the roles in the Microsoft Entra ID token by customizing the token issuance in Microsoft Entra ID.

    Why it's wrong here

    You cannot dynamically change the Microsoft Entra ID token contents from your application. Token customization is done at Microsoft Entra ID level and is not suitable for dynamic, application-specific roles.

  • Use the [Authorize] attribute with a custom authorization filter that checks the database on every request without modifying the claims.

    Why it's wrong here

    While this could work, it is less efficient and does not integrate well with claims-based authorization. Adding the roles as claims is the recommended pattern because it allows policies to work declaratively.

Common exam traps

Common exam trap: answer the scenario, not the keyword

The trap here is that candidates often assume Entra ID app roles or groups are the only way to implement role-based authorization, overlooking the flexibility of the OnTokenValidated event to inject custom claims from external sources.

Trap categories for this question

  • Scenario analysis trap

    The scenario explicitly states that roles are not defined in Microsoft Entra ID and are dynamic. Relying on app roles would not work because the roles are not in the token.

Detailed technical explanation

How to think about this question

Under the hood, the OpenID Connect middleware's OnTokenValidated event fires after the token is cryptographically validated but before the ClaimsIdentity is fully established. By adding claims here (e.g., via `context.Principal.Identities.First().AddClaim(new Claim(ClaimTypes.Role, "Admin"))`), the claims become part of the user's identity for the entire request pipeline, enabling seamless integration with policy-based authorization. In real-world scenarios, this pattern is essential for multi-tenant SaaS applications where role mappings vary per tenant and are stored in a tenant-specific database.

KKey Concepts to Remember

  • Read the scenario before looking for a memorised answer.
  • Find the constraint that changes the correct option.
  • Eliminate answers that are true in general but not in this case.

TExam Day Tips

  • Watch for words such as best, first, most likely and least administrative effort.
  • Review why wrong options are wrong, not only why the correct option is correct.

Key takeaway

Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.

Real-world example

How this comes up in practice

A company's IT admin needs to give a contractor read-only access to production logs without sharing account credentials. Using role-based access control (RBAC) and temporary scoped permissions — not a permanent shared password — is the correct pattern. Questions like this test whether you can apply least-privilege access across cloud identity services.

What to study next

Got this wrong? Here's your next step.

Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.

Related practice questions

Related AZ-204 practice-question pages

Use these pages to review the topic behind this question. This is how one missed question becomes focused revision.

Practice this exam

Start a free AZ-204 practice session

Short sessions build daily habit. Longer sessions build exam-day stamina. Try a timed session to simulate real conditions.

FAQ

Questions learners often ask

What does this AZ-204 question test?

Implement Azure security — This question tests Implement Azure security — Read the scenario before looking for a memorised answer..

What is the correct answer to this question?

The correct answer is: Use the OnTokenValidated event in OpenID Connect middleware to query the database and add custom role claims to the identity. — Option B is correct because it uses the OnTokenValidated event in OpenID Connect middleware to enrich the user's identity with custom role claims from the application database after token validation. This approach allows dynamic role mappings stored externally to be injected into the ClaimsPrincipal, which can then be evaluated by the standard [Authorize] attribute with role policies. It avoids modifying Entra ID configuration and keeps role management flexible within the application.

What should I do if I get this AZ-204 question wrong?

Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.

What is the key concept behind this question?

Read the scenario before looking for a memorised answer.

About these practice questions

Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Same concept, more angles

1 more ways this is tested on AZ-204

These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.

Variation 1. You are developing an ASP.NET Core web API that authenticates users via Microsoft Entra ID. The application needs to authorize access to resources based on custom roles (e.g., 'Admin', 'Editor') that are not present in Microsoft Entra ID. The role mappings are dynamic and stored in an application database. How should you implement authorization?

hard
  • A.Define the roles as Microsoft Entra ID app roles and include them in the token claims.
  • B.Store the role mappings in an Azure SQL Database and use a custom authorization policy that queries the database after authentication.
  • C.Include the roles as claims in the Microsoft Entra ID token by using a custom claim mapping policy.
  • D.Store the role mappings in the web.config file and read them at runtime.

Why B: Option B is correct because the custom roles are dynamic and stored in an application database, not in Microsoft Entra ID. After authentication, a custom authorization policy can query the database to retrieve the role mappings for the authenticated user and enforce access control. This approach decouples role management from the identity provider and supports dynamic role assignments.

Last reviewed: Jun 11, 2026

Question Discussion

Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.

Loading comments…

Sign in to join the discussion.

This AZ-204 practice question is part of Courseiva's free Microsoft certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the AZ-204 exam.