63 lines
1.7 KiB
C#
63 lines
1.7 KiB
C#
using System.Collections.ObjectModel;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using Entidades.Dto;
|
|
using Entidades;
|
|
|
|
namespace Modelo;
|
|
|
|
public class RepositorioUsuarios: RepositorioBase<RepositorioUsuarios>
|
|
{
|
|
public bool AltaCliente(CrearClienteDto cid){
|
|
var usu = cid.Usuario;
|
|
var cli = cid.Cliente;
|
|
|
|
var cant = Context.Usuarios
|
|
.GroupBy(u => u.id)
|
|
.Select(x => x.Count())
|
|
.ToList();
|
|
if (cant.Count < 1) return false;
|
|
|
|
usu.id = cant[0];
|
|
cli.idusuario = cant[0];
|
|
|
|
Context.Usuarios.Add(usu);
|
|
Context.Clientes.Add(cli);
|
|
return Guardar();
|
|
}
|
|
public bool CheckUsuario(LoginDto logindto) {
|
|
|
|
string Contraseña = HacerHash(logindto.Contraseña);
|
|
|
|
Usuario? usu = Context.Usuarios.FirstOrDefault(a => a.email == logindto.Email);
|
|
if (usu == null) return false;
|
|
|
|
string hashdb = Encoding.UTF8.GetString(usu.contraseña);
|
|
|
|
if (hashdb == Contraseña) return true;
|
|
return false;
|
|
|
|
}
|
|
private string HacerHash(string pass){
|
|
var buf = SHA256.HashData(Encoding.UTF8.GetBytes(pass));
|
|
return BitConverter.ToString(buf).Replace("-","");
|
|
}
|
|
|
|
public bool CheckToken(TokenDto token){
|
|
var usu = Context.Usuarios.FirstOrDefault(x => x.email == token.Email);
|
|
if (usu == null) return false;
|
|
|
|
return usu.token == token.Token;
|
|
|
|
}
|
|
|
|
public void GuardarToken(LoginDto login, string tokenString)
|
|
{
|
|
var usu = Context.Usuarios.FirstOrDefault(x => x.email == login.Email);
|
|
if (usu == null) return;
|
|
usu.token = tokenString;
|
|
Guardar();
|
|
}
|
|
|
|
}
|