Control number of user accesses in Windows Forms

Asked

Viewed 94 times

0

I am creating an application in Windows Forms and would like to control so that users log in to a single station that is running the application. In an earlier version, my solution was to keep a shared folder on a server and when the user entered the application I opened a txt file for editing with the user name. This way, when it was going to open from another station, when the application tried to write that file it generated an error. I would like to change this format because today not all stations have access to the same shared folder.

  • 1

    Have you ever thought of controlling a table in the database?

  • I thought this way: I would create a record when the user accessed it and delete it when closing the system. But if the system shuts down unexpectedly, the registry would be created and block the user from accessing again.

1 answer

1


You can create a column in your database of type boolean for when it is logged in.

Adding the table:

ALTER TABLE tabela_usuarios ADD 'logado' boolean;

To put Boolean as true When you open the form, you can use the event Form_Load() in this way:

private void Form1_Load(object sender, EventArgs e)
{
    if (checklogado())
    {
        string comando = "UPDATE tabela_usuarios SET logado=1 WHERE usuario=@Usuario";
        var cmd = new MySqlCommand(comando, connection); //connection é a string da conexão.
        cmd.Parameters.AddWithValue("@Usuario", usuario); //usuario seria o nome de usuário da pessoa logada.
        cmd.ExecuteScalar();
    }
    else
    {
        MessageBox.Show("Você já está logado em outra máquina!", "Erro");
        Environment.Exit(0);
    }
}

bool checklogado()
{
    string comando = "SELECT logado FROM tabela_usuarios WHERE usuario=@Usuario";
    var cmd = new MySqlCommand(comando, connection);
    cmd.Parameters.AddWithValue("@Usuario", usuario);
    bool returno = Convert.ToBoolean(cmd.ExecuteScalar());
    return retorno;
}

To put Boolean as false when closing the form, you can use the event Form_FormClosing() in this way:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    string comando = "UPDATE tabela_usuarios SET logado=0 WHERE usuario=@Usuario";
    var cmd = new MySqlCommand(comando, connection); //connection é a string da conexão.
    cmd.Parameters.AddWithValue("@Usuario", usuario); //usuario seria o nome de usuário da pessoa logada.
    cmd.ExecuteScalar();
}
  • You still need to include a check in the Load event and if the user is already on-site, it displays a message and does not log in. I also recommend storing date/time and machine that the user logged in.

  • @Rodolphosa thanks for the tip, I edited my reply.

  • The problem is that some fault occurs and the system shuts down improperly. Then the user will not be able to re-enter!

  • You can make a column for the time logged in to the section and an event in which the user is automatically logged out when it exceeds 3 hours.

Browser other questions tagged

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