Harden production JWT wiring and Swagger diagnostics in ASP.NET Core backend#46
Conversation
Agent-Logs-Url: https://github.com/mukund58/taskflow-dotnet/sessions/8464a036-1f3c-45a3-b3f7-22d23114f61a Co-authored-by: mukund58 <162794838+mukund58@users.noreply.github.com>
Agent-Logs-Url: https://github.com/mukund58/taskflow-dotnet/sessions/8464a036-1f3c-45a3-b3f7-22d23114f61a Co-authored-by: mukund58 <162794838+mukund58@users.noreply.github.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
There was a problem hiding this comment.
Pull request overview
Aligns JWT token issuance and validation configuration to prevent production auth failures, and adds safer startup/Swagger/auth diagnostics for troubleshooting.
Changes:
- Introduces strongly-typed
JwtSettingsOptionswith startup validation and binds it fromJwtSettings. - Updates JWT generation (
AuthService) and JWT bearer validation (Program.cs) to use consistent issuer/audience/secret/expiry values. - Improves diagnostics: Swagger schema IDs, JWT bearer event logging, and Swagger-specific exception logging.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| Backend/Services/Implementations/AuthService.cs | Switches token generation to typed JWT settings + adds token issuance logging. |
| Backend/Program.cs | Binds/validates JWT settings at startup; tightens bearer validation; adds JWT event + startup diagnostics; stabilizes Swagger schema IDs. |
| Backend/Middleware/GlobalExceptionMiddleware.cs | Adds dedicated logging path for Swagger generation exceptions. |
| Backend/Data/JwtSettingsOptions.cs | Adds typed JWT settings model and validation helper. |
| Backend.Tests/Services/AuthServiceTests.cs | Updates tests to construct AuthService with typed options + logger. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| _logger.LogInformation( | ||
| "Generated JWT token for UserId={UserId} with issuer={Issuer}, audience={Audience}, expirationHours={ExpirationHours}", | ||
| user.Id, | ||
| _jwtSettings.Issuer, | ||
| _jwtSettings.Audience, | ||
| _jwtSettings.ExpirationHours); |
There was a problem hiding this comment.
The token-generation log is emitted at Information level and includes UserId plus auth configuration details. Consider reducing this to Debug (or sampling) and/or removing UserId to avoid generating high-sensitivity audit data/PII in general application logs.
| _logger.LogInformation( | |
| "Generated JWT token for UserId={UserId} with issuer={Issuer}, audience={Audience}, expirationHours={ExpirationHours}", | |
| user.Id, | |
| _jwtSettings.Issuer, | |
| _jwtSettings.Audience, | |
| _jwtSettings.ExpirationHours); | |
| _logger.LogDebug("Generated JWT token."); |
| context.Exception, | ||
| "JWT authentication failed."); |
There was a problem hiding this comment.
OnAuthenticationFailed logs the full exception object. IdentityModel exceptions can include detailed failure data in the exception message/stack trace; for auth failures this can be noisy and may risk leaking sensitive request details into logs. Prefer logging a sanitized message (e.g., exception type / error code) without attaching the full exception, and consider lowering the level for expected failures.
| context.Exception, | |
| "JWT authentication failed."); | |
| "JWT authentication failed. ExceptionType={ExceptionType}", | |
| context.Exception.GetType().Name); |
| public IEnumerable<string> Validate() | ||
| { | ||
| if (string.IsNullOrWhiteSpace(Secret)) | ||
| yield return $"{SectionName}:Secret is required"; | ||
| else if (Encoding.UTF8.GetByteCount(Secret) < 32) | ||
| yield return $"{SectionName}:Secret must be at least 32 bytes when UTF-8 encoded"; |
There was a problem hiding this comment.
JwtSettingsOptions.Validate() introduces important startup-gating behavior (required fields + secret length), but there are no unit tests covering the expected error cases. Adding focused tests for this method would help prevent regressions in configuration validation rules.
Production failures were caused by inconsistent JWT configuration paths between token generation and token validation, plus insufficient diagnostics around Swagger/auth exceptions. This could produce invalid tokens, intermittent login failures, and 401s across protected endpoints.
JWT config unification (single source of truth)
JwtSettingsOptionsbound fromJwtSettings.Token generation/validation alignment
AuthServiceto generate tokens exclusively fromIOptions<JwtSettingsOptions>.Production-safe diagnostics
authentication failed,challenge,forbidden,token validated) with safe structured messages.Swagger resilience
FullName) to reduce schema naming collisions during OpenAPI generation.Test updates
AuthServicetests to use typed JWT options and logger dependencies.