Example #1
0
        public ActionResult Create(Propietario propietario)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var prop  = repositorio.ObtenerPorEmail(propietario.Email);
                    var user  = repoUsuario.ObtenerPorEmail(propietario.Email);
                    var inqui = repoInquilino.ObtenerPorEmail(propietario.Email);

                    if (user == null && inqui == null && prop == null)
                    {
                        propietario.Clave = Convert.ToBase64String(KeyDerivation.Pbkdf2(
                                                                       password: propietario.Clave,
                                                                       salt: System.Text.Encoding.ASCII.GetBytes(config["Salt"]),
                                                                       prf: KeyDerivationPrf.HMACSHA1,
                                                                       iterationCount: 1000,
                                                                       numBytesRequested: 256 / 8));
                        repositorio.Alta(propietario);
                        TempData["Id"] = propietario.Id;
                        return(RedirectToAction(nameof(Index)));
                    }
                    else
                    {
                        TempData["Error"] = "El Email ingresado ya se encuentra registrado en el sistema! ";
                        ViewBag.Error     = TempData["Error"];
                        return(View());
                    }
                }
                else
                {
                    return(View(propietario));
                }
            }
            catch (Exception ex)
            {
                ViewBag.Error      = ex.Message;
                ViewBag.StackTrace = ex.StackTrace;
                return(View(propietario));
            }
        }
Example #2
0
        public ActionResult Create(Usuario u)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    ViewBag.Roles = Usuario.ObtenerRoles();
                    if (u.Id == 0)
                    {
                        TempData["Mensaje"] = "Debe ingresar todo los datos del usuario!";
                    }
                    ViewBag.Error = TempData["Mensaje"];
                    return(View());
                }
                {
                    var user  = repositorio.ObtenerPorEmail(u.Email);
                    var inqui = repoInquilino.ObtenerPorEmail(u.Email);
                    var prop  = repoPropietario.ObtenerPorEmail(u.Email);

                    if (user == null && inqui == null && prop == null)
                    {
                        string hashed = Convert.ToBase64String(KeyDerivation.Pbkdf2(
                                                                   password: u.Clave,
                                                                   salt: System.Text.Encoding.ASCII.GetBytes(configuration["Salt"]),
                                                                   prf: KeyDerivationPrf.HMACSHA1,
                                                                   iterationCount: 1000,
                                                                   numBytesRequested: 256 / 8));
                        u.Clave = hashed;
                        u.Rol   = User.IsInRole("Administrador") ? u.Rol : (int)enRoles.Empleado;
                        var nbreRnd = Guid.NewGuid();//posible nombre aleatorio
                        int res     = repositorio.Alta(u);
                        TempData["Id"] = u.Id;
                        return(RedirectToAction(nameof(Index)));
                    }
                    else
                    {
                        TempData["Mensaje"] = "El Email ingresado ya se encuentra registrado en el sistema! ";
                        ViewBag.Error       = TempData["Mensaje"];
                        ViewBag.Roles       = Usuario.ObtenerRoles();
                        return(View());
                    }
                }
            }
            catch (Exception ex)
            {
                ViewBag.Error      = ex.Message;
                ViewBag.StackTrate = ex.StackTrace;
                ViewBag.Roles      = Usuario.ObtenerRoles();

                return(View());
            }
        }
Example #3
0
        public ActionResult Create(Inquilino inquilino)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var inqui = repositorio.ObtenerPorEmail(inquilino.Email);
                    var user  = repoUsuario.ObtenerPorEmail(inquilino.Email);
                    var prop  = repoPropietario.ObtenerPorEmail(inquilino.Email);

                    if (user == null && prop == null && (inqui == null || inqui.Email == inquilino.Email))
                    {
                        repositorio.Alta(inquilino);
                        TempData["Id"] = inquilino.Id;
                        return(RedirectToAction(nameof(Index)));
                    }
                    else
                    {
                        TempData["Error"] = "El Email ingresado ya se encuentra registrado en el sistema! ";
                        ViewBag.Error     = TempData["Error"];
                        return(View(inquilino));
                    }
                }
                else
                {
                    ViewBag.Propietarios = repositorio.ObtenerTodos();
                    return(View(inquilino));
                }
            }
            catch (Exception ex)
            {
                ViewBag.Error      = ex.Message;
                ViewBag.StackTrate = ex.StackTrace;
                return(View(inquilino));
            }
        }
Example #4
0
        public async Task <ActionResult> Login(LoginView loginView)
        {
            try
            {
                string hashed = Convert.ToBase64String(KeyDerivation.Pbkdf2(
                                                           password: loginView.Clave,
                                                           salt: System.Text.Encoding.ASCII.GetBytes(config["Salt"]),
                                                           prf: KeyDerivationPrf.HMACSHA1,
                                                           iterationCount: 1000,
                                                           numBytesRequested: 256 / 8));
                var p = propietarios.ObtenerPorEmail(loginView.Usuario);
                if (p == null || p.Clave != hashed)
                {
                    ViewBag.Mensaje = "Datos inválidos";
                    return(View());
                }
                var claims = new List <Claim>
                {
                    new Claim(ClaimTypes.Name, p.Email),
                    new Claim("FullName", p.Nombre + " " + p.Apellido),
                    //new Claim(ClaimTypes.Role, p.IdPropietario < 10? "Administrador":"Propietario"),
                    new Claim(ClaimTypes.Role, "Administrador"),
                };

                var claimsIdentity = new ClaimsIdentity(
                    claims, CookieAuthenticationDefaults.AuthenticationScheme);

                var authProperties = new AuthenticationProperties
                {
                    //AllowRefresh = <bool>,
                    // Refreshing the authentication session should be allowed.
                    AllowRefresh = true,
                    //ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(10),
                    // The time at which the authentication ticket expires. A
                    // value set here overrides the ExpireTimeSpan option of
                    // CookieAuthenticationOptions set with AddCookie.

                    //IsPersistent = true,
                    // Whether the authentication session is persisted across
                    // multiple requests. When used with cookies, controls
                    // whether the cookie's lifetime is absolute (matching the
                    // lifetime of the authentication ticket) or session-based.

                    //IssuedUtc = <DateTimeOffset>,
                    // The time at which the authentication ticket was issued.

                    //RedirectUri = <string>
                    // The full path or absolute URI to be used as an http
                    // redirect response value.
                };

                await HttpContext.SignInAsync(
                    CookieAuthenticationDefaults.AuthenticationScheme,
                    new ClaimsPrincipal(claimsIdentity),
                    authProperties);

                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                ViewBag.Error      = ex.Message;
                ViewBag.StackTrate = ex.StackTrace;
                return(View());
            }
        }
        public async Task <ActionResult> Login(LoginView loginView)
        {
            try
            {
                string hashed = Convert.ToBase64String(KeyDerivation.Pbkdf2(
                                                           password: loginView.Password,
                                                           salt: System.Text.Encoding.ASCII.GetBytes(config["Salt"]),
                                                           prf: KeyDerivationPrf.HMACSHA1,
                                                           iterationCount: 1000,
                                                           numBytesRequested: 256 / 8));
                var p = propietarios.ObtenerPorEmail(loginView.Email);

                if (loginView.Email == "*****@*****.**" && loginView.Password == "admin")
                {
                    var claims = new List <Claim> {
                        new Claim(ClaimTypes.Name, p.Email),
                        new Claim("FullName", p.Nombre + " " + p.Apellido),
                        new Claim(ClaimTypes.Role, "Administrador"),
                    };
                    var claimsIdentity = new ClaimsIdentity(
                        claims, CookieAuthenticationDefaults.AuthenticationScheme);
                    var authProperties = new AuthenticationProperties

                    {
                        AllowRefresh = true,
                    };

                    await HttpContext.SignInAsync(
                        CookieAuthenticationDefaults.AuthenticationScheme,
                        new ClaimsPrincipal(claimsIdentity),
                        authProperties);

                    return(RedirectToAction("Index"));
                }
                else if (loginView.Email == p.Email && p.Password == hashed)
                {
                    var claims = new List <Claim> {
                        new Claim(ClaimTypes.Name, p.Email),
                        new Claim("FullName", p.Nombre + " " + p.Apellido),
                        new Claim(ClaimTypes.Role, "Usuario"),
                    };
                    var claimsIdentity = new ClaimsIdentity(
                        claims, CookieAuthenticationDefaults.AuthenticationScheme);
                    var authProperties = new AuthenticationProperties
                    {
                        AllowRefresh = true,
                    };

                    await HttpContext.SignInAsync(
                        CookieAuthenticationDefaults.AuthenticationScheme,
                        new ClaimsPrincipal(claimsIdentity),
                        authProperties);

                    return(RedirectToAction("permitidos"));
                }
                else
                {
                    ViewBag.Mensaje = "Correo o Contraseña Incorrectos!";
                    return(View());
                }
            }
            catch (Exception ex)
            {
                ViewBag.Error      = ex.Message;
                ViewBag.StackTrate = ex.StackTrace;
                return(View());
            }
        }