π 30 Tricky Interview Questions on Authentication & Authorization in .NET Core Web API – With Detailed Answers π‘
Are you preparing for a .NET Core interview or looking to master secure Web API development? Here's your go-to guide with 30 advanced Authentication & Authorization questions, perfect for cracking interviews or leveling up your skills. ✅
π Authentication vs Authorization
-
What is the difference between Authentication and Authorization?
-
Authentication verifies who you are.
-
Authorization defines what you can access.
-
Example: Logging in = Authentication; Viewing dashboard = Authorization.
-
π JWT Token-Based Authentication
-
What is JWT and why is it used in Web API?
-
JWT (JSON Web Token) is a compact, URL-safe token used to transfer claims between two parties. It’s widely used for stateless authentication.
-
-
How do you generate JWT in .NET Core?
-
Use
System.IdentityModel.Tokens.Jwt
. -
Configure
TokenValidationParameters
and generate token usingJwtSecurityTokenHandler
.
-
-
What are claims in JWT?
-
Claims are key-value pairs that represent user identity (e.g.,
Name
,Email
,Role
).
-
-
What is the purpose of the 'Issuer' and 'Audience' in JWT?
-
Issuer
: Who created the token. -
Audience
: Who the token is intended for.
-
π§© Role-Based Authorization
-
How is role-based authorization implemented in .NET Core?
-
Decorate actions or controllers using
[Authorize(Roles = "Admin")]
.
-
-
Can a user have multiple roles in JWT?
-
Yes. Use multiple
"role"
claims or an array of roles in the JWT payload.
-
-
How do you secure endpoints for multiple roles?
π‘️ Policy-Based Authorization
-
What is policy-based authorization?
-
Instead of static roles, policies allow custom logic via requirements and handlers.
-
-
How do you define a custom policy?
-
Use
services.AddAuthorization(options => {...})
and register requirements.
-
How do you implement a custom AuthorizationHandler?
-
Inherit from
AuthorizationHandler<TRequirement>
and overrideHandleRequirementAsync
.
π Token Refresh & Expiry
-
What happens when a JWT token expires?
-
The client gets a
401 Unauthorized
. You can implement a refresh token strategy.
-
How do you implement Refresh Tokens?
-
Store refresh tokens server-side. On expiry, send a new access token using the refresh token.
π OAuth2 & OpenID Connect
-
What is the difference between OAuth2 and OpenID Connect?
-
OAuth2 is for authorization.
-
OpenID Connect is built on OAuth2 and adds authentication features.
-
How to implement Google/Facebook login in .NET Core?
-
Use
AddAuthentication().AddGoogle()
orAddFacebook()
inStartup.cs
.
π IdentityServer & External Providers
-
What is IdentityServer?
-
A powerful framework for implementing OAuth2 and OpenID Connect in .NET Core.
-
How do you secure an API using IdentityServer4?
-
Configure IdentityServer, generate tokens, and validate them in the Web API using middleware.
π¦ ASP.NET Core Identity
-
What is ASP.NET Core Identity?
-
A full-featured membership system for managing users, passwords, roles, and claims.
-
How do you override password rules in Identity?
-
Configure
PasswordOptions
inIdentityOptions
during service configuration.
π Middleware & Token Validation
-
Where should you validate JWT tokens in the middleware pipeline?
-
After
UseRouting()
but beforeUseEndpoints()
.
-
How can you access the currently logged-in user?
-
Use
HttpContext.User.Identity.Name
or access claims fromUser.Claims
.
π§ Advanced & Tricky Scenarios
-
Can you invalidate a JWT token before it expires?
-
JWTs are stateless, so you must implement token revocation via a server-side blacklist.
-
How to prevent token replay attacks?
-
Use short-lived tokens with refresh strategy, and track usage on the server.
-
Is HTTPS required for JWT?
-
Yes. JWTs contain sensitive info and must be transmitted over HTTPS.
-
How to secure Swagger endpoints?
-
Use
[Authorize]
on controllers and configure Swagger to accept Bearer tokens.
-
What’s the risk of putting roles in the JWT?
-
If the token is compromised, all role info is exposed. Always encrypt tokens and use HTTPS.
π Multi-Tenancy & Claims
-
How to implement multi-tenancy authorization?
-
Include tenant info in claims and validate it in custom handlers or filters.
-
How do you manage per-tenant roles?
-
Use custom claims like
TenantId
and role mapping logic based on tenant.
⚙️ Miscellaneous
-
Difference between
[Authorize]
,[AllowAnonymous]
, and[Authorize(Roles = "...")]
?
-
[Authorize]
: Requires authentication. -
[AllowAnonymous]
: Skips auth. -
[Authorize(Roles = "...")]
: Requires specific role.
-
How to log authorization failures?
-
Hook into
OnChallenge
orOnForbidden
events in JWT bearer options.
π¨π» Bonus Tips
-
Always secure APIs using HTTPS.
-
Rotate keys and secrets regularly.
-
Prefer short-lived tokens with refresh strategy.
-
Validate tokens strictly (issuer, audience, lifetime, signing key).
π¬ Let me know in the comments which question was most helpful!
π Like & π Repost to help others ace their .NET Core interviews!
Comments
Post a Comment