示例#1
0
        public async Task <IActionResult> Ingresar(string username, string password, string returnUrl)
        {
            if (!string.IsNullOrWhiteSpace(username) && !string.IsNullOrWhiteSpace(password))
            {
                Usuario usuario = _context.Profesor.FirstOrDefault(usr => usr.Username == username);
                if (usuario == null)
                {
                    usuario = _context.Alumno.FirstOrDefault(usr => usr.Username == username);
                }
                if (usuario != null)
                {
                    byte[] passwordEncriptada = new SHA256Managed().ComputeHash(Encoding.ASCII.GetBytes(password));

                    if (usuario.Password.SequenceEqual(passwordEncriptada))
                    {
                        // Se crean las credenciales del usuario que serán incorporadas al contexto
                        ClaimsIdentity identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
                        // El lo que luego obtendré al acceder a User.Identity.Name
                        identity.AddClaim(new Claim(ClaimTypes.Name, username));
                        // Se utilizará para la autorización por roles
                        identity.AddClaim(new Claim(ClaimTypes.Role, usuario.Role.ToString()));
                        identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, usuario.Id.ToString()));

                        ClaimsPrincipal principal = new ClaimsPrincipal(identity);

                        // En este paso se hace el login del usuario al sistema
                        await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);

                        // Guardo la fecha de último acceso que es ahora.
                        usuario.FechaUltimoAcceso = DateTime.Now;
                        _context.SaveChanges();

                        if (!string.IsNullOrWhiteSpace(returnUrl))
                        {
                            return(Redirect(returnUrl));
                        }

                        TempData["primerLogin"] = true;

                        return(RedirectToAction(nameof(HomeController.Index), "Home"));
                    }
                }
            }
            // Completo estos dos campos para poder retornar a la vista en caso de errores.
            ViewBag.Error     = "Usuario o contraseña incorrectos";
            ViewBag.UserName  = username;
            ViewBag.ReturnUrl = returnUrl;

            return(View());
        }
示例#2
0
 private void Seed()
 {
     if (!_context.Profesor.Any())
     {
         _context.Add(new Profesor()
         {
             Username = "******",
             Role     = Role.Administrador,
             Password = "******".Encriptar()
         });
         _context.SaveChanges();
     }
     if (!_context.Alumno.Any())
     {
         _context.Add(new Alumno()
         {
             Username = "******",
             Role     = Role.Cliente,
             Password = "******".Encriptar()
         });
         _context.SaveChanges();
     }
 }