TFS_ANTIGUO/TPAtesa_RecHuma/REC-HUMA.INFRASTRUCTURE/Repositories/JwtAuthenticationManager.cs
2026-06-26 10:14:39 -06:00

104 lines
No EOL
3.1 KiB
C#

//using Microsoft.IdentityModel.Tokens;
//using REC_HUMA.CORE.Interfaces;
//using System;
//using System.Collections.Generic;
//using System.IdentityModel.Tokens.Jwt;
//using System.Linq;
//using System.Security.Claims;
//using System.Text;
//using System.Threading.Tasks;
//namespace REC_HUMA.INFRASTRUCTURE.Repositories
//{
// public class JwtAuthenticationManager : IJwtAuthenticationManager
// {
// private readonly IDictionary<string, string> users = new Dictionary<string, string>
// { { "test1", "password1"}, {"test2", "password2"} };
// private readonly string key;
// public JwtAuthenticationManager(string key)
// {
// this.key = key;
// }
// public string Authenticate(string username, string password)
// {
// if (!users.Any(u => u.Key == username && u.Value == password))
// {
// return null;
// }
// var tokenHandler = new JwtSecurityTokenHandler();
// var tokenKey = Encoding.ASCII.GetBytes(key);
// var tokenDescriptor = new SecurityTokenDescriptor
// {
// Subject = new ClaimsIdentity(new Claim[]
// {
// new Claim(ClaimTypes.Name, username)
// }),
// Expires = DateTime.UtcNow.AddHours(1),
// SigningCredentials =
// new SigningCredentials(
// new SymmetricSecurityKey(tokenKey),
// SecurityAlgorithms.HmacSha256Signature)
// };
// var token = tokenHandler.CreateToken(tokenDescriptor);
// return tokenHandler.WriteToken(token);
// }
// }
//}
using Microsoft.IdentityModel.Tokens;
using REC_HUMA.CORE.Interfaces;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Text;
namespace REC_HUMA.API.JWT
{
public class JwtAuthenticationManager : IJwtAuthenticationManager
{
private readonly string key;
public JwtAuthenticationManager(string key)
{
this.key = key;
}
public string Authenticate(string NombreUsuario, string Contraseña)
{
if (string.IsNullOrEmpty(NombreUsuario) || string.IsNullOrEmpty(Contraseña) || NombreUsuario != "Demo" || Contraseña != "1234567")
{
return null;
}
var tokenHandler = new JwtSecurityTokenHandler();
var tokenKey = Encoding.ASCII.GetBytes(key);
var tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(new Claim[]
{
new Claim(ClaimTypes.Name, NombreUsuario)
}),
Expires = DateTime.UtcNow.AddHours(1),
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(tokenKey), SecurityAlgorithms.HmacSha256Signature)
};
var token = tokenHandler.CreateToken(tokenDescriptor);
return tokenHandler.WriteToken(token);
}
}
}