Lesson 8 of 8
Mini Project: Auth System - Register, Login, Refresh
Build a complete authentication system: Register (with email confirmation), Login (with 2FA option), refresh token rotation via httpOnly cookies, forced logout (all devices), password reset, and Google social login - production-ready auth from day one.
Quick Quiz - JWT Authentication & ASP.NET Identity
Complete Auth System Endpoints
// ━━ Auth API surface ━━
// POST /api/auth/register → create account, send verification email
// POST /api/auth/confirm-email → verify email token from link
// POST /api/auth/login → returns access_token + sets refresh_token cookie
// POST /api/auth/refresh → rotate refresh token, return new access_token
// POST /api/auth/logout → revoke refresh token family, clear cookie
// POST /api/auth/forgot-password → send reset link
// POST /api/auth/reset-password → apply new password
// GET /api/auth/me → current user info (protected)
// POST /api/auth/2fa/setup → get TOTP setup QR code (protected)
// POST /api/auth/2fa/verify → enable 2FA (protected)
// GET /api/auth/google → redirect to Google OAuth
// GET /api/auth/google-callback → handle Google OAuth callback
// ━━ /me endpoint - get current user from JWT ━━
[HttpGet("me"), Authorize]
public async Task<IActionResult> GetCurrentUser()
{
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier)!;
var user = await userManager.FindByIdAsync(userId);
if (user is null) return NotFound();
var roles = await userManager.GetRolesAsync(user);
var claims = await userManager.GetClaimsAsync(user);
return Ok(new
{
user.Id,
user.Email,
user.FirstName,
user.LastName,
user.FullName,
EmailConfirmed = user.EmailConfirmed,
TwoFactorEnabled = await userManager.GetTwoFactorEnabledAsync(user),
Roles = roles,
Claims = claims.Select(c => new { c.Type, c.Value })
});
}
// ━━ Security checklist (all implemented by this module) ━━
// ✅ Passwords: bcrypt via Identity (Argon2 via custom IPasswordHasher<T>)
// ✅ JWT: 15-minute access tokens, 7-day refresh tokens
// ✅ Refresh tokens: httpOnly, Secure, SameSite=Strict cookies
// ✅ Token rotation: old token invalidated on each refresh
// ✅ Replay detection: revoke entire family on token reuse
// ✅ Token revocation: Redis JTI blacklist for forced logout
// ✅ Account lockout: 5 failed attempts → 15-minute lockout
// ✅ Email confirmation: unconfirmed users cannot log in
// ✅ Security stamp: password change/ban invalidates all sessions
// ✅ 2FA: TOTP via Authenticator app
// ✅ Social login: Google OAuth2 (no password at all)
// ✅ Rate limiting: /auth/* endpoints use "auth" policy (5 req/15min)
class ApplicationUser { public string Id { get; set; } = ""; public bool EmailConfirmed { get; set; } public string FirstName { get; set; } = ""; public string LastName { get; set; } = ""; public string? Email { get; set; } public string FullName => $"{FirstName} {LastName}".Trim(); }
UserManager<ApplicationUser> userManager = null!;Tip
Tip
Practice Mini Project Auth System Register Login Refresh in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
.NET provides a unified platform for building various application types
Practice Task
Note
Practice Task - (1) Write a working example of Mini Project Auth System Register Login Refresh from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Common Mistake
Warning
A common mistake with Mini Project Auth System Register Login Refresh is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready dotnet code.
Finished this lesson?