Overview
SGRH uses JWT (JSON Web Tokens) for stateless authentication. The system implements token-based authentication with refresh token support, allowing secure user login and session management.JWT Configuration
Configure JWT Settings
Add JWT configuration to your
appsettings.json:appsettings.json
The issuer of the JWT token. Typically your application name or domain.Example:
SGRH-Production, https://api.sgrh.comThe intended audience for the token. Should match your client application.Example:
SGRH-Web, SGRH-MobileSecret key for signing JWT tokens. Must be at least 256 bits (32 characters) for HS256.
Token expiration time in minutes. Common values:
- Development: 120 minutes (2 hours)
- Production: 15-60 minutes
Install JWT Bearer Package
The SGRH.Api project already includes the required package:If not installed, add it via:
SGRH.Api.csproj
Token Generation
Create a service to generate JWT tokens:JwtTokenService.cs
Program.cs
Login Implementation
Example login endpoint:AuthController.cs
Protecting Endpoints
Use the[Authorize] attribute to protect endpoints:
ReservasController.cs
Accessing User Claims
Retrieve authenticated user information from claims:Refresh Token Implementation
For enhanced security, implement refresh tokens:Environment-Specific Settings
- Development
- Production
appsettings.Development.json
CORS Configuration
If your frontend is on a different domain, configure CORS:Program.cs
Testing Authentication
Troubleshooting
401 Unauthorized
- Verify token is included in
Authorization: Bearer <token>header - Check token hasn’t expired
- Ensure
UseAuthentication()is called beforeUseAuthorization()
403 Forbidden
- User is authenticated but doesn’t have required role/permissions
- Check
[Authorize(Roles = "...")]configuration
Invalid Signature
- JWT secret key mismatch between token generation and validation
- Verify
Jwt:Keyis identical in all environments
Next Steps
Database Setup
Configure SQL Server and Entity Framework Core
AWS Services
Integrate S3 storage and SES email services