ASP.NET Core Identity Basics

>

๐Ÿ’ก Concept Name

ASP.NET Core Identity

๐Ÿ“˜ Quick Intro

ASP.NET Core Identity is a full-featured authentication and user management system. It simplifies user registration, login, password storage, and role-based access. It's built on Entity Framework Core and integrates tightly with ASP.NET Core apps.

๐Ÿง  Analogy / Short Story

Think of Identity like the receptionist in a secure office building. It manages the employee records (users), verifies their ID on entry (login), and grants access to rooms based on roles. It also keeps a logbook (claims) and ensures your badge (password) is secure and encrypted.

๐Ÿ”ง Technical Explanation

Identity provides a `UserManager`, `SignInManager`, and `RoleManager` to manage authentication and authorization. Users are stored in a database via Entity Framework Core. It also supports token generation, email confirmation, and 2FA. You can scaffold Identity UI or customize views using Razor Pages.

๐ŸŽฏ Purpose & Use Case

  • โœ… Enable user registration and login securely
  • โœ… Store hashed passwords and manage user roles
  • โœ… Support external login providers (Google, Facebook, etc.)
  • โœ… Enable account lockout, 2FA, and email confirmation
  • โœ… Secure web apps with built-in authorization and claims

๐Ÿ’ป Real Code Example

// Add Identity in Program.cs
builder.Services.AddIdentity<IdentityUser, IdentityRole>()
    .AddEntityFrameworkStores<ApplicationDbContext>()
    .AddDefaultTokenProviders();

// Protect route with Identity
[Authorize]
public class ProfileController : Controller
{
    public IActionResult Index() => View();
}

โ“ Interview Q&A

Q1: What is ASP.NET Core Identity?
A: A built-in system for user authentication, registration, and role management.

Q2: What are the key services in Identity?
A: `UserManager`, `SignInManager`, `RoleManager`.

Q3: Where are users stored?
A: In the database using Entity Framework Core.

Q4: How are passwords stored?
A: Hashed using secure algorithms like PBKDF2.

Q5: What is the purpose of claims?
A: Claims hold additional user data like department or country.

Q6: Can Identity be used in Razor Pages?
A: Yes, it fully supports Razor Pages.

Q7: How to add external login support?
A: Use services like `.AddGoogle()`, `.AddFacebook()` in Program.cs.

Q8: What is `AddDefaultTokenProviders()` for?
A: It enables email confirmation, password reset, etc.

Q9: Can we customize Identity UI?
A: Yes, by scaffolding Identity pages.

Q10: What are roles used for?
A: To control access by grouping users based on permissions.

๐Ÿ“ MCQs

๐Ÿ“ MCQs

Q1. Which service handles login logic in Identity?

  • UserService
  • IdentityCore
  • SignInManager
  • LoginHandler

Q2. What is stored in the AspNetUsers table?

  • Sessions
  • Role claims
  • User accounts
  • Controllers

Q3. How are passwords stored?

  • Plain text
  • Base64
  • Hashed
  • Encrypted

Q4. Which class assigns roles to users?

  • UserManager
  • ClaimsHandler
  • RoleManager
  • TokenProvider

Q5. Which method adds Identity in Program.cs?

  • ConfigureIdentity
  • AddIdentity
  • UseIdentity
  • EnableLogin

Q6. What attribute protects a controller using Identity?

  • [Identity]
  • [Secure]
  • [Authorize]
  • [Protected]

Q7. Which provider allows email confirmation?

  • MailKit
  • SMTP
  • DefaultTokenProviders
  • GoogleToken

Q8. Can Identity work without a database?

  • Yes
  • No
  • Only in-memory
  • Only with NoSQL

Q9. Which table holds roles?

  • UserRoles
  • RoleTable
  • AspNetRoles
  • RoleClaims

Q10. What does SignInManager.PasswordSignInAsync do?

  • Logs out user
  • Creates user
  • Logs in a user
  • Resets password

๐Ÿ’ก Bonus Insight

ASP.NET Core Identity is customizable โ€” you can use your own user class, database schema, or even swap out EF Core. It's secure by default but also extensible for advanced scenarios like claims transformation and policy-based access control.

๐Ÿ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

๐Ÿ’ฌ Feedback
๐Ÿš€ Start Learning
Share:

Tags: