Example #1
0
        public ActionResult Index(FormCollection collection)
        {
            openConn();
            string email = collection["Email"].ToString();
            string pass  = collection["Contrasenya"].ToString();

            pass = Codifica.ConverteixPassword(pass);
            try
            {
                string sql =
                    "SELECT * FROM usuarios WHERE Email=@email and contrasenya=@password";
                MySqlCommand cmd = new MySqlCommand();
                cmd.CommandText = sql;
                cmd.Connection  = _conn;
                cmd.Parameters.AddWithValue("@email", email);
                cmd.Parameters.AddWithValue("@password", pass);
                cmd.Prepare();
                MySqlDataReader rdr   = cmd.ExecuteReader();
                Usuario         Model = null;
                if (rdr.Read())
                {
                    int _id = 0;
                    Int32.TryParse(rdr["idUsuario"].ToString(), out _id);
                    Model             = new Usuario();
                    Model.Id          = Convert.ToInt32(rdr["idUsuario"]);
                    Model.Nombre      = rdr["Nombre"].ToString();
                    Model.Contrasenya = rdr["contrasenya"].ToString();;
                    Model.Email       = rdr["Email"].ToString();
                }

                rdr.Close();
                closeConn(); //método propio que cierra conexión si está abierta

                if (Model != null)
                {
                    this.HttpContext.Session.Add("logged", Model.Nombre);
                    this.HttpContext.Session.Add("loggedid", Model.Id.ToString());
                    return(RedirectToAction("Index", "Home"));
                }
            }
            catch (Exception ex)
            {
                closeConn(); //método propio que cierra conexión si está abierta
            }

            return(View());
        }
Example #2
0
        public ActionResult Registro(FormCollection collection)
        {
            // primer verifiquem que email NO existeixi
            // si existeix, retornem a vista registre amb msg d'error
            // cal crear mètode: bool emailExisteix(string email)
            if (emailExisteix(collection["Email"].ToString()))
            {
                ViewBag.ErrorMsg = "Este email ya ha sido registrado";
                return(RedirectToAction("Index", "Login"));
            }

            try
            {
                _conn.Open();
                MySqlCommand cmd = new MySqlCommand();
                string       sql =
                    "INSERT INTO usuarios (Nombre, contrasenya, Email) VALUES (@nom,@password,@email)";

                string passwordVisible    = collection["contrasenya"];
                string passwordCodificada =
                    Codifica.ConverteixPassword(passwordVisible);

                cmd.CommandText = sql;
                cmd.Parameters.AddWithValue("@nom", collection["Nombre"].ToString());
                cmd.Parameters.AddWithValue("@password", passwordCodificada);
                cmd.Parameters.AddWithValue("@email", collection["Email"].ToString());
                //para coger el id del logueado que se acaba de registrar
                //last_insert_id() as ultimo

                cmd.Connection = _conn;
                cmd.Prepare();
                cmd.ExecuteNonQuery();
                _conn.Close();

                //this.HttpContext.Session.Add("logged", collection["Nombre"].ToString());
                //this.HttpContext.Session.Add("loggedid", 2);
                return(RedirectToAction("Index", "Login"));
            }
            catch (Exception e)
            {
                if (_conn.State == System.Data.ConnectionState.Open)
                {
                    _conn.Close();
                }
                return(View());
            }
        }