Get the logged in user id

Asked

Viewed 82 times

0

I found many questions of this kind here in Sopt but none that specifically talked about C# in Winforms.

Well, I have a login screen and would like to pick up the id of that user who is currently logged in to do a type of report that does not come to the point now. I believe that the most efficient would be to create a Session but I have no idea how to do it in C#.

Method created to verify login:

public bool VerificaLogin()
    {
        bool result = false;
        string StringDeConexao = "SERVER = localhost; DATABASE = global; UID = root; PASSWORD=";

            using (MySqlConnection cn = new MySqlConnection())
            {
                cn.ConnectionString = StringDeConexao;

                try
                {
                        MySqlCommand cmd = new MySqlCommand("SELECT * FROM usuarios WHERE login = '" + txtlogin.Text + "'AND senha = '" + GerarHashMd5(txtsenha.Text) +  "';", cn);
                        cn.Open();
                        MySqlDataReader dados = cmd.ExecuteReader();

                        result = dados.HasRows;
                }
                catch (MySqlException e)
                {
                    MetroFramework.MetroMessageBox.Show(this, "" + e.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                finally
                {
                    cn.Close();
                }
            }
            return result;
    }

    public bool Logado = false;

button enter:

        private void btnentrar_Click(object sender, EventArgs e)
        {
           bool result = VerificaLogin();

           Logado = result;

           if (result)
           {
               MetroFramework.MetroMessageBox.Show(this, "Seja Bem-vindo", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
               this.Close();
           }
           else
           {
               MetroFramework.MetroMessageBox.Show(this, "Usuário e/ou senha incorretos", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Warning);
           }
       }

1 answer

0

In the Verificalogin() method you can change it to return an integer, if the login is correct, it would return the user ID, if the login is not done, it can return a negative number to represent the error.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.