Backend Setup & Installation
Relevant source files
The following files were used as context for generating this wiki page:
The backend of this project is built using .NET Core, specifically targeting the .NET 8.0 SDK. It serves as a RESTful API providing essential services for user management, product cataloging, and secure authentication. The system is designed with a service-oriented architecture, utilizing Entity Framework Core for data persistence and JWT (JSON Web Tokens) for secure, stateless authentication.
The primary scope of the backend is to handle business logic, enforce security policies via CORS and Identity settings, and provide a documented interface through Swagger/OpenAPI. This allows the frontend React application to interact with data through standardized HTTP methods while maintaining data integrity and security.
Core Environment Requirements
The backend requires a specific development environment to ensure compatibility and successful compilation.
.NET SDK Configuration
The project is pinned to a specific version of the .NET SDK to maintain consistency across development environments.
| Setting | Value | Description |
|---|---|---|
| SDK Version | 8.0.0 | Primary .NET SDK version |
| Roll Forward | latestMinor | Allows using newer minor versions of the 8.0 SDK |
| Prerelease | false | Disallows use of prerelease SDK versions |
Sources: Back/Alumnos/global.json:1-7
Server Profiles and URLs
The application supports multiple hosting profiles, including Kestrel (HTTP/HTTPS) and IIS Express.
| Profile | Command | Application URL | Environment |
|---|---|---|---|
| http | Project | http://localhost:5230 | Development |
| https | Project | https://localhost:7170 | Development |
| IIS Express | IISExpress | http://localhost:33744 | Development |
Sources: Back/Alumnos/Api/Properties/launchSettings.json:10-40
Architecture and Middleware Configuration
The backend initialization is managed in the entry point of the application, where services are registered and the HTTP request pipeline is configured.
Dependency Injection and Services
The system utilizes Dependency Injection (DI) to manage service lifetimes and resolve dependencies for controllers.
- Database Context: Registered using
AddDbContextwith SQL Server support. The connection string is retrieved from the configuration keyDefaultConexion. - Security Services:
IHasherServicio: Implemented bySha512HasherServiciofor password hashing.ITokenIdentidadServicio: Implemented byJwtTokenServiciofor JWT management.
- CORS Policy: Configured under "PoliticasCors" to allow any origin, header, and method, specifically exposing the
Authorizationheader for frontend token handling.
Sources: Back/Alumnos/Api/Program.cs:15-22, Back/Alumnos/Api/Program.cs:45-51
Middleware Pipeline Flow
The following diagram illustrates how requests are processed through the backend middleware.
This flow ensures that security and policy checks are performed before the request reaches the business logic in the controllers.
Sources: Back/Alumnos/Api/Program.cs:100-105
Authentication and Security
The backend implements JWT Bearer authentication to secure its endpoints.
JWT Configuration
Authentication is configured to validate issuer signing keys using a secret key defined in the IdentidadAjustes section.
builder.Services.AddAuthentication(auth =>
{
auth.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(opt =>
{
opt.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(identidadAjustes.Secreto)),
ValidateIssuer = false,
ValidateAudience = false,
ValidateLifetime = true,
ClockSkew = TimeSpan.Zero,
};
});
Sources: Back/Alumnos/Api/Program.cs:52-73
Token Validation Logic
During the OnTokenValidated event, the system performs a secondary validation check through the ITokenIdentidadServicio to ensure the token hasn't been revoked or invalidated by custom logic.
Sources: Back/Alumnos/Api/Program.cs:74-94
API Exposure and Documentation
The backend uses Swagger (OpenAPI) to document available endpoints and facilitate testing.
Swagger Configuration
The API documentation is configured to support Bearer Token authentication directly within the Swagger UI.
- Security Definition: Defines "Bearer" as an ApiKey located in the Header with the name "Authorization".
- Launch URL: By default, the application launches to the
/swaggerendpoint in development mode.
Sources: Back/Alumnos/Api/Program.cs:23-44, Back/Alumnos/Api/Properties/launchSettings.json:14
Frontend Integration
The frontend services are designed to interact with these backend endpoints, typically targeting the base URL defined in the backend launch settings.
| Service | Method | Endpoint | Description |
|---|---|---|---|
usuariosService | GET | /usuarios | Fetch users with filters |
usuariosService | POST | /usuarios | Register new user |
productosService | GET | /productos | List available products |
authService | POST | /login | Authenticate and receive JWT |
Sources: Front/src/services/api.js:27-124, Front/src/services/api.ts:55-139
Conclusion
The backend setup provides a robust foundation for the application, leveraging .NET 8's performance and Entity Framework's data management capabilities. Through the use of JWT for security and Swagger for documentation, it ensures that the system is both secure and accessible for frontend developers. Precise environment configuration via global.json and launchSettings.json facilitates a standardized installation process across the development team.