public ActionResult Create(Propietario propietario)
        {
            try
            {
                var existeCorreoPropietario = repositorio.ObtenerPorCorreo(propietario.Correo);

                if (ModelState.IsValid && existeCorreoPropietario == null)
                {
                    if (empleado.ObtenerPorCorreo(propietario.Correo) != null)
                    {
                        //el correo ya exite champ
                        return(View());
                    }
                    else
                    {
                        propietario.Clave = Convert.ToBase64String(KeyDerivation.Pbkdf2(
                                                                       password: propietario.Clave,
                                                                       salt: System.Text.Encoding.ASCII.GetBytes("SALADA"),
                                                                       prf: KeyDerivationPrf.HMACSHA1,
                                                                       iterationCount: 1000,
                                                                       numBytesRequested: 256 / 8));
                        repositorio.Alta(propietario);
                        if (TempData.ContainsKey("Id"))
                        {
                            ViewBag.Id = TempData["Id"];
                        }
                        ViewBag.MensajeError = null;
                        ViewBag.Exito        = "Propietario registrado con exito";
                        return(View());
                    }
                }
                else
                if (TempData.ContainsKey("Id"))
                {
                    ViewBag.Id = TempData["Id"];
                }
                ViewBag.MensajeError = "No che, sabes que te faltó algo";
                return(View());
            }
            catch (Exception ex)
            {
                if (TempData.ContainsKey("Id"))
                {
                    ViewBag.Id = TempData["Id"];
                }
                ViewBag.Error        = ex.Message;
                ViewBag.StackTrate   = ex.StackTrace;
                ViewBag.MensajeError = "No sabemos que pasó pero hiciste algo mal seguro.";
                return(View());
            }
        }
Exemplo n.º 2
0
        public ActionResult Create(Empleado empleado)
        {
            try
            {
                var existeCorreoEmpleado = repositorio.ObtenerPorCorreo(empleado.Correo);

                if (ModelState.IsValid && existeCorreoEmpleado == null)
                {
                    if (propietario.ObtenerPorCorreo(empleado.Correo) != null)
                    {
                        //este correo ya está en uso y este software no permite los mismo correos :(
                        if (TempData.ContainsKey("Id"))
                        {
                            ViewBag.Id = TempData["Id"];
                        }
                        ViewBag.Mensaje = "Lamentamos informate que no podés elegir este correo. Intenta con otro por favor.";
                        return(View());
                    }
                    else
                    {
                        empleado.Clave = Convert.ToBase64String(KeyDerivation.Pbkdf2(
                                                                    password: empleado.Clave,
                                                                    salt: System.Text.Encoding.ASCII.GetBytes("SALADA"),
                                                                    prf: KeyDerivationPrf.HMACSHA1,
                                                                    iterationCount: 1000,
                                                                    numBytesRequested: 256 / 8));
                        repositorio.Alta(empleado);
                        return(RedirectToAction(nameof(Index)));
                    }
                }
                else
                if (TempData.ContainsKey("Id"))
                {
                    ViewBag.Id = TempData["Id"];
                }
                ViewBag.Mensaje = "Campo vacío y/o correo no disponible. Intente otro";
                return(View());
            }
            catch (Exception ex)
            {
                ViewBag.Error      = ex.Message;
                ViewBag.StackTrate = ex.StackTrace;
                if (TempData.ContainsKey("Id"))
                {
                    ViewBag.Id = TempData["Id"];
                }
                ViewBag.Mensaje = "No sabemos que pasó pero hiciste algo mal seguro.";
                return(View());
            }
        }
Exemplo n.º 3
0
        public async Task <ActionResult> Login(LoginView loginView)
        {
            try
            {
                if (loginView.Usuario.Equals(null) || loginView.Clave.Equals(null))
                {
                    ViewBag.Mensaje = "Datos inválidos";
                    return(View());
                }
                else
                {
                    string hashed = Convert.ToBase64String(KeyDerivation.Pbkdf2(
                                                               password: loginView.Clave,
                                                               salt: System.Text.Encoding.ASCII.GetBytes("SALADA"),
                                                               prf: KeyDerivationPrf.HMACSHA1,
                                                               iterationCount: 1000,
                                                               numBytesRequested: 256 / 8));
                    var e = empleadosRepo.ObtenerPorCorreo(loginView.Usuario);
                    var p = propietarios.ObtenerPorCorreo(loginView.Usuario);

                    if (e == null || e.Clave != hashed)
                    {
                        if (p == null || p.Clave != hashed)
                        {
                            ViewBag.Mensaje = "Datos inválidos";
                            return(View());
                        }
                        else
                        //es propietario
                        {
                            var claims = new List <Claim>
                            {
                                new Claim(ClaimTypes.Name, p.Correo),
                                new Claim("FullName", p.Nombre + " " + p.Apellido),
                                new Claim(ClaimTypes.Role, "Propietario"),
                            };

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

                            var authProperties = new AuthenticationProperties
                            {
                                //AllowRefresh = <bool>,
                                // Refreshing the authentication session should be allowed.
                                AllowRefresh = true,
                            };

                            TempData["Id"] = p.IdPropietario;

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

                            return(RedirectToAction("VistaPropietarioIndex", "Propietario"));
                        }
                    }
                    else
                    //es empelado
                    {
                        var claims = new List <Claim>
                        {
                            new Claim(ClaimTypes.Name, e.Correo),
                            new Claim("FullName", e.Nombre + " " + e.Apellido),
                            new Claim(ClaimTypes.Role, "Empleado"),
                        };

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

                        var authProperties = new AuthenticationProperties
                        {
                            //AllowRefresh = <bool>,
                            // Refreshing the authentication session should be allowed.
                            AllowRefresh = true,
                        };

                        TempData["Id"] = e.IdEmpleado;

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

                        return(RedirectToAction("Index", "Empleado"));
                    }
                }
            }
            catch (Exception ex)
            {
                ViewBag.Mensaje = ex.StackTrace;
                return(View());
            }
        }