Backend Configuration
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
The backend configuration of this project is built using ASP.NET Core, providing a robust infrastructure for handling authentication, database persistence, and API services. It follows a modular architecture where services are registered via Dependency Injection, and security is enforced through JWT (JSON Web Token) bearer authentication.
The system is designed to support a frontend application (React-based) by exposing RESTful endpoints for user management, product catalogs, and messaging. The backend manages environment-specific settings through JSON configuration files and leverages Entity Framework Core for database operations.
Sources: Back/Alumnos/Api/Program.cs, Front/src/services/api.js
System Architecture and Middleware
The backend startup process in Program.cs configures the web application builder, defining how the system handles requests, security, and data.
Core Service Registration
The application uses a variety of service lifetimes to manage dependencies:
- Singleton:
IdentidadAjusteis registered to hold global identity settings. - Scoped:
AplicacionBdContextohandles database context per request. - Transient:
IHasherServicio(Sha512) andITokenIdentidadServicio(JWT) are created each time they are requested.
Sources: Back/Alumnos/Api/Program.cs:15-21
Authentication and Security Flow
Security is centered around JWT. The system validates the IssuerSigningKey using a secret key defined in the configuration. It also includes custom event handling during token validation to ensure the token has not been revoked or expired via the ITokenIdentidadServicio.
The diagram above illustrates the authentication pipeline where every request must pass through standard JWT checks followed by a custom service validation logic.
Sources: Back/Alumnos/Api/Program.cs:56-94
Environment and Launch Settings
The backend supports multiple execution profiles, primarily focusing on development environments with Swagger integration for API documentation.
| Profile | Command | URL | Environment |
|---|---|---|---|
| http | Project | http://localhost:5230 | Development |
| https | Project | https://localhost:7170; http://localhost:5230 | Development |
| IIS Express | IISExpress | Variable (Internal) | Development |
Sources: Back/Alumnos/Api/Properties/launchSettings.json:13-43
API Service Structure
The frontend interacts with the backend through specialized services defined in both JavaScript and TypeScript. These services map to the backend controllers.
Service Modules
- UsuariosService: Manages user profiles, registration, and status (enabled/disabled).
- ProductosService: Handles CRUD operations for the marketplace products.
- AuthService: Manages login/logout and token persistence in
localStorage.
Sources: Front/src/services/api.js, Front/src/services/api.ts
Request Interception and Headers
Both the JS and TS implementations include logic to automatically attach the Bearer token to outgoing requests.
const getAuthHeader = () => {
const token = localStorage.getItem('token');
return token ? { 'Authorization': `Bearer ${token}` } : {};
};
Sources: Front/src/services/api.js:18-21, Front/src/services/api.ts:38-41
Persistence Configuration
The backend uses Entity Framework Core with SQL Server. Specific entity configurations are used to define primary keys and relationships.
Database Configuration Examples
| Entity | Key Property | Description |
|---|---|---|
| Categoria | CategoriaID | Defines the unique identifier for product categories. |
| SesionUsuario | Id | Defines the primary key for user session tracking. |
Sources: Back/Alumnos/Api/Persistencia/Configuraciones/CategoriaConfiguracion.cs:11, Back/Alumnos/Api/Persistencia/Configuraciones/SesionUsuarioConfiguracion.cs:11
CORS and Request Handling
To allow communication between the React frontend (typically on port 3000) and the .NET backend (port 5230), a CORS policy named "PoliticasCors" is implemented. This policy allows any origin, header, and method, while specifically exposing the Authorization header so the frontend can retrieve new tokens.
Sources: Back/Alumnos/Api/Program.cs:48-54
Summary
The backend configuration provides a secure and scalable foundation for the web application. By utilizing JWT for identity, Entity Framework for data persistence, and a well-defined service layer in the frontend, the system ensures that user data and product information are handled consistently and securely across the platform.