Пример #1
0
        public IActionResult Registro(RegistroViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                if (_repoUsuario.EstaDisponible(model.Correo))
                {
                    ModelState.AddModelError(string.Empty, "Correo no disponible");
                    return(View(model));
                }

                var usuario = _repoUsuario.Create(new Usuario
                {
                    Nombre          = model.Nombre,
                    PrimerApellido  = model.PrimerApellido,
                    SegundoApellido = model.SegundoApellido,
                    NumeroCelular   = model.NumeroCelular,
                    NombreUsuario   = model.Correo,
                    Correo          = model.Correo,
                    HashContrasena  = HashString.Encrypt(model.Contrasena),
                    FechaNacimiento = model.FechaNacimiento
                });


                if (usuario.Id <= 0)
                {
                    ModelState.AddModelError(string.Empty, "Error al generar el usuario");
                    return(View(model));
                }

                return(RedirectToLocal(returnUrl));
            }

            return(View(model));
        }
Пример #2
0
        public async Task <IActionResult> LogIn(LogInViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                string hashModel = HashString.Encrypt(model.Contrasena);
                string hashBd    = _repoUsuario.ObtenerHash(model.Cuenta);

                if (hashModel.Equals(hashBd))
                {
                    var usuario = _repoUsuario.LeerUsuario(model.Cuenta);

                    #region snippet1
                    var claims = new List <Claim>
                    {
                        new Claim(ClaimTypes.Name, usuario.Correo),
                        new Claim("FullName", usuario.ToString())
                    };

                    foreach (var claim in usuario.Especificaciones)
                    {
                        claims.Add(new Claim(claim.Nombre, claim.Valor));
                    }

                    foreach (var rol in usuario.Roles)
                    {
                        claims.Add(new Claim(ClaimTypes.Role, rol.Nombre));
                    }

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

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

                        //ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(30),
                        // 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. Required when setting the
                                       // ExpireTimeSpan option of CookieAuthenticationOptions
                                       // set with AddCookie. Also required when setting
                                       // ExpiresUtc.

                                       //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);

                    #endregion

                    return(RedirectToAction(nameof(HomeController.Index), "Home"));
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Usuario o contraseña incorrectos");
                    return(View(model));
                }
            }
            return(View());
        }