public ActionResult Create(Usuario u) { try { IList <Usuario> usuario = usuarioData.ObtenerTodos(); foreach (var item in (usuario)) { if (u.Mail == item.Mail) { ViewBag.Error = "Error: Ya existe un usuario con ese email"; ViewBag.roles = RolesData.getAll(); return(View(u)); } } if (ModelState.IsValid) { string hashed = Convert.ToBase64String(KeyDerivation.Pbkdf2( password: u.Pass, salt: System.Text.Encoding.ASCII.GetBytes(configuration["Salt"]), prf: KeyDerivationPrf.HMACSHA1, iterationCount: 1000, numBytesRequested: 256 / 8 )); u.Pass = hashed; u.RolId = User.IsInRole("Administrador") ? u.RolId : 2; int res = usuarioData.Alta(u); var nombreAleatorio = Guid.NewGuid(); if (u.AvatarFile != null && res > 0) { string wwwPath = environment.WebRootPath; string path = Path.Combine(wwwPath, "Uploads"); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string fileName = "avatar_" + u.Id + Path.GetExtension(u.AvatarFile.FileName); string pathCompleto = Path.Combine(path, fileName); u.Avatar = Path.Combine("/Uploads", fileName); using (FileStream stream = new FileStream(pathCompleto, FileMode.Create)) { u.AvatarFile.CopyTo(stream); } usuarioData.Modificacion(u); } return(RedirectToAction(nameof(Index))); } else { return(View(u)); } } catch (Exception e) { Console.WriteLine("Estoy en la excepcion"); return(View()); } }