The DonaSF API uses JSON Web Tokens (JWT) for authentication. This guide covers how to obtain, use, and manage authentication tokens in your applications.
The Token object returned from authentication endpoints:
public class Token{ public string Tokens { get; set; } // The JWT string public string Identificador { get; set; } // Email or phone number public DateTime Expiracion { get; set; } // Token expiration date public int IdCliente { get; set; } // Client ID public string Nombre { get; set; } // Client name public bool Activo { get; set; } // Account active status}
var claims = new List<Claim>(){ new Claim("IdCliente", idCliente.ToString()), new Claim("Identificador", identificador), new Claim("correo", correo), new Claim("telefono", telefono), new Claim("nombre", nombre), new Claim("Contrasena", contrasena)};
Security Warning: The current implementation includes the password in the token claims. This is a security risk and should be removed in production environments.
var client = new HttpClient();// Set the Authorization header with the Bearer tokenclient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Tokens);// Make authenticated requestsvar response = await client.GetAsync("http://localhost:5005/Donacion/VerDonaciones");
The Expiracion field in the token response indicates when the token will expire. Monitor this field and refresh the token before expiration.
const token = { "Tokens": "eyJhbGci...", "Expiracion": "2026-03-13T12:00:00Z"};const expirationDate = new Date(token.Expiracion);const now = new Date();const isExpired = now > expirationDate;if (isExpired) { // Re-authenticate to get a new token console.log('Token expired, please login again');}
The API does not currently support token refresh. When a token expires, you must re-authenticate using the /Cliente/Login endpoint to obtain a new token.
Best Practice: Implement automatic token refresh in your application by checking expiration time and logging in again before the token expires.