implementado login a travez de coockies
This commit is contained in:
@@ -21,17 +21,29 @@ public class LoginController: ControllerBase
|
||||
string tokenString = GenerarToken(loginDto);
|
||||
RepositorioUsuarios.Singleton.GuardarToken(loginDto, tokenString);
|
||||
|
||||
return Ok( new {Email = loginDto.Email, Token = tokenString, Redirect = "/Menu"});
|
||||
var cookieOptions = new CookieOptions
|
||||
{
|
||||
HttpOnly = true,
|
||||
Secure = true,
|
||||
//SameSite = SameSiteMode.Strict,
|
||||
Expires = DateTimeOffset.UtcNow.AddHours(1)
|
||||
};
|
||||
|
||||
Response.Cookies.Append("token", tokenString, cookieOptions);
|
||||
return Ok( new {Email = loginDto.Email, Redirect = "/Menu"});
|
||||
}
|
||||
|
||||
[HttpPost("api/login/validar")]
|
||||
public IActionResult Verificar([FromBody] TokenDto tokenRequest){
|
||||
if (tokenRequest.Email == String.Empty ||tokenRequest.Token == string.Empty ||tokenRequest.Redirect == string.Empty)
|
||||
public IActionResult Verificar([FromBody] AccessDto request){
|
||||
|
||||
Request.Cookies.TryGetValue("token", out var token);
|
||||
|
||||
if (request.Email == String.Empty || token == null ||request.Redirect == string.Empty)
|
||||
{
|
||||
return Unauthorized(new { esValido = false});
|
||||
}
|
||||
|
||||
bool esValido = RepositorioUsuarios.Singleton.CheckToken(tokenRequest);
|
||||
bool esValido = RepositorioUsuarios.Singleton.CheckToken(request.Email, token);
|
||||
return (esValido) ?
|
||||
Ok( new { esValido = true}) : Unauthorized( new {esValido = false});
|
||||
}
|
||||
|
||||
@@ -11,9 +11,10 @@ builder.Services.AddCors(options =>
|
||||
options.AddPolicy("AllowSvelteApp",
|
||||
builder =>
|
||||
{
|
||||
builder.AllowAnyOrigin()
|
||||
builder.WithOrigins("http://localhost:5173")
|
||||
.AllowAnyHeader()
|
||||
.AllowAnyMethod();
|
||||
.AllowAnyMethod()
|
||||
.AllowCredentials();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
namespace Entidades.Dto;
|
||||
|
||||
public class TokenDto{
|
||||
public class AccessDto {
|
||||
public string Email { get; set; } = null!;
|
||||
public string Token {get; set;} = String.Empty;
|
||||
public string Redirect { get; set; } = String.Empty;
|
||||
}
|
||||
@@ -10,7 +10,7 @@
|
||||
export let component;
|
||||
|
||||
let redirect = window.location.pathname;
|
||||
const token = localStorage.getItem('token');
|
||||
|
||||
const email = localStorage.getItem('email');
|
||||
|
||||
const handleAccess = async () => {
|
||||
@@ -20,7 +20,8 @@
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify( {email, token, redirect} ),
|
||||
body: JSON.stringify( {email, redirect} ),
|
||||
credentials: "include"
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
|
||||
@@ -18,7 +18,8 @@
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(data)
|
||||
body: JSON.stringify(data),
|
||||
credentials: "include"
|
||||
});
|
||||
|
||||
if (!response.ok){
|
||||
@@ -30,7 +31,6 @@
|
||||
|
||||
const ret = await response.json();
|
||||
localStorage.clear();
|
||||
localStorage.setItem('token', ret.token);
|
||||
localStorage.setItem('email', ret.email);
|
||||
//setTimeout(() => console.log("50ms") ,50);
|
||||
navigate(ret.redirect);
|
||||
|
||||
@@ -7,22 +7,19 @@ namespace Modelo;
|
||||
public abstract class RepositorioBase<S>
|
||||
where S : new()
|
||||
{
|
||||
protected AlquilaFacilContext Context { get; set; } = new AlquilaFacilContext();
|
||||
|
||||
protected AlquilaFacilContext Context { get{ return new AlquilaFacilContext();}}
|
||||
private static readonly S instance = new();
|
||||
public static S Singleton { get{return instance;}}
|
||||
|
||||
public bool Guardar(){
|
||||
public bool Guardar(AlquilaFacilContext context){
|
||||
bool ret = false;
|
||||
try
|
||||
{
|
||||
Context.SaveChanges();
|
||||
Context.Dispose();
|
||||
Context = new AlquilaFacilContext();
|
||||
context.SaveChanges();
|
||||
context.Dispose();
|
||||
ret = true;
|
||||
} catch (DbUpdateException ex)
|
||||
{
|
||||
Context = new AlquilaFacilContext();
|
||||
Console.Error.WriteLine(ex.Message);
|
||||
}
|
||||
return ret;
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
using Modelo;
|
||||
|
||||
public class RepositorioInquilinos: RepositorioBase<RepositorioInquilinos>
|
||||
{
|
||||
|
||||
}
|
||||
9
Modelo/RepositorioPropiedades.cs
Normal file
9
Modelo/RepositorioPropiedades.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using Modelo;
|
||||
|
||||
public class RepositorioPropiedades: RepositorioBase<RepositorioPropiedades>
|
||||
{
|
||||
|
||||
public bool AñadirPropiedad(){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -3,55 +3,46 @@ using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using Entidades.Dto;
|
||||
using Entidades;
|
||||
using System.Reflection.Metadata.Ecma335;
|
||||
|
||||
namespace Modelo;
|
||||
|
||||
public class RepositorioUsuarios: RepositorioBase<RepositorioUsuarios>
|
||||
{
|
||||
public bool AltaCliente(CrearClienteDto cid){
|
||||
var usu = new Usuario {
|
||||
email = cid.email,
|
||||
contraseña = Encoding.UTF8.GetBytes(HacerHash(cid.contraseña))
|
||||
};
|
||||
|
||||
var cli = new Cliente {
|
||||
dni = cid.dni,
|
||||
nombre = cid.nombre,
|
||||
domicilio = cid.domicilio,
|
||||
apellido = cid.apellido,
|
||||
celular = cid.celular
|
||||
Dni = cid.dni,
|
||||
Nombre = cid.nombre,
|
||||
Domicilio = cid.domicilio,
|
||||
Apellido = cid.apellido,
|
||||
Celular = cid.celular,
|
||||
Email = cid.email,
|
||||
Contraseña = Encoding.UTF8.GetBytes(HacerHash(cid.contraseña))
|
||||
};
|
||||
|
||||
var cant = Context.Usuarios
|
||||
.GroupBy(u => u.id)
|
||||
.Select(x => x.Count())
|
||||
.ToList();
|
||||
if (cant.Count < 1) return false;
|
||||
|
||||
usu.id = cant.Count() + 1;
|
||||
cli.idusuario = cant.Count() + 1;
|
||||
|
||||
var grupo = Context.Grupos.Find(2);
|
||||
if (grupo == null || grupo.id == 0) return false;
|
||||
var con = Context;
|
||||
var grupo = con.Grupos.Find(2);
|
||||
if (grupo == null || grupo.Id == 0) return false;
|
||||
|
||||
Context.Usuarios.Add(usu);
|
||||
Guardar();
|
||||
con.Clientes.Add(cli);
|
||||
Guardar(con);
|
||||
|
||||
con = Context;
|
||||
cli = con.Clientes.Find(cli.Dni) ?? new();
|
||||
if (cli.Dni == 0) return false;
|
||||
|
||||
cli.Idgrupos.Add(grupo);
|
||||
return Guardar(con);
|
||||
|
||||
var usut = Context.Usuarios.Find(usu.id);
|
||||
usut.idgrupos.Add(grupo);
|
||||
Guardar();
|
||||
|
||||
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);
|
||||
Cliente? usu = Context.Clientes.FirstOrDefault(a => a.Email == logindto.Email);
|
||||
if (usu == null) return false;
|
||||
|
||||
string hashdb = Encoding.UTF8.GetString(usu.contraseña);
|
||||
string hashdb = Encoding.UTF8.GetString(usu.Contraseña);
|
||||
|
||||
if (hashdb == Contraseña) return true;
|
||||
return false;
|
||||
@@ -62,20 +53,20 @@ public class RepositorioUsuarios: RepositorioBase<RepositorioUsuarios>
|
||||
return BitConverter.ToString(buf).Replace("-","");
|
||||
}
|
||||
|
||||
public bool CheckToken(TokenDto token){
|
||||
var usu = Context.Usuarios.FirstOrDefault(x => x.email == token.Email);
|
||||
public bool CheckToken(string email, string token){
|
||||
var usu = Context.Clientes.FirstOrDefault(x => x.Email == email);
|
||||
if (usu == null) return false;
|
||||
|
||||
return usu.token == token.Token;
|
||||
|
||||
return usu.Token == token;
|
||||
}
|
||||
|
||||
public void GuardarToken(LoginDto login, string tokenString)
|
||||
{
|
||||
var usu = Context.Usuarios.FirstOrDefault(x => x.email == login.Email);
|
||||
var con = Context;
|
||||
var usu = con.Clientes.FirstOrDefault(x => x.Email == login.Email);
|
||||
if (usu == null) return;
|
||||
usu.token = tokenString;
|
||||
Guardar();
|
||||
usu.Token = tokenString;
|
||||
Guardar(con);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user