public async Task<ActionResult> InicioSesion(UsuarioInicioSesion model, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            // This doesn't count login failures towards account lockout
            // To enable password failures to trigger account lockout, change to shouldLockout: true
            var result = await SignInManager.PasswordSignInAsync(model.email, model.password, model.RememberMe, shouldLockout: false);
            switch (result)
            {
                case SignInStatus.Success:
                    Usuario usuario = new Usuario();
                    try
                    {
                        usuario = db.Usuarios.Single(u => u.email.Equals(model.email));
                    }
                    catch (Exception)
                    {
                        ModelState.AddModelError("", "Invalid login attempt.");
                        return View(model);

                    }
                    UsuarioRol usuarioRol = db.UsuarioRols.Single(r => r.id_usuario == usuario.ID);
                     if (usuarioRol.id_rol == 1)
                     {
                        return RedirectToAction("Index", "Home");
                    }
                      else
                      {                        
                        return RedirectToLocal(returnUrl);
                    }
                   
                case SignInStatus.LockedOut:
                    return View("Lockout");
                case SignInStatus.RequiresVerification:
                    return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
                case SignInStatus.Failure:
                default:
                    ModelState.AddModelError("", "Invalid login attempt.");
                    return View(model);
            }
        }
        public ActionResult DeleteConfirmed(int id)
        {
            Usuario usuario = new Usuario();
            try
            {
                 usuario = db.Usuarios.Find(id);
            }
            catch (Exception)
            {

               
            }
            try
            {
                db.Usuarios.Remove(usuario);
            }
            catch (Exception)
            {

               
            }
            try
            {
                db.SaveChanges();
            }
            catch (Exception)
            {

               
            }
            return RedirectToAction("Index");
        }
        public ActionResult cambiarCiudadUsuario(string town, Usuario usuario)
        {
            usuario.ciudad = town;
            try
            {
                db.Entry(usuario).State = EntityState.Modified;
            }
            catch (Exception)
            {

                throw;
            }
            try
            {
                db.SaveChanges();
            }
            catch (Exception)
            {
                throw;
            }

            return RedirectToAction("Index", "Manage");
        }
        public async Task<ActionResult> Registro (Usuario model)
        {
            int ciu = Convert.ToInt32(model.ciudad);
            Ciudad ciudad = db.Ciudads.Single(c => c.ID.Equals(ciu));

            model.ciudad = ciudad.nombre;

            if (ModelState.IsValid)
            {
                
                try
                {
                    db.Usuarios.Add(model);
                }
                catch (Exception)
                {

                }
                try
                {
                    db.SaveChanges();
                }
                catch (Exception)
                {


                }
                int id = model.ID;
                UsuarioRol usuarioRol = new UsuarioRol(2, id);
                try
                {
                    db.UsuarioRols.Add(usuarioRol);
                }
                catch (Exception)
                {
                }
                try
                {
                    db.SaveChanges();
                }
                catch (Exception)
                {
                }

                var user = new ApplicationUser { UserName = model.email, Email = model.email };
                var result = await UserManager.CreateAsync(user, model.password);
                if (result.Succeeded)
                {
                    await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
                    
                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    return RedirectToAction("Index", "Home");

                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }