I need to control user sessions, for this reason I have thought about the login to ask if there is any session in the database related to the user that is being achieved.
If an active session exists, you must ask if you want to change it (if you change or your session is not active, continue with the following)
If the user wants to change the session, the following is done:
-
Execute a random that generates a random number and pass it to the main window.
-
Then a timer is generated that must be executed every 2 minutes repetitively and when it reaches the end, it must perform a select to the database with a randomly generated number through a Random (already generate the number) and check if the number is still the same or different.
-
If the number is the same, nothing should be done
-
If the number is different, the application must be closed (windows form).
I have the idea, but I need programmatic guidance.
(so far I need to program the timer with the select) the timer must be asynchronous so that the application does not hang while performing the select
private void bunifuThinButton21_Click(object sender, EventArgs e)
{
try
{
borramensajeerror();
if (validacion())
{
erroricono.Clear();
if (txt_rut.Text.Equals("") || txt_contraseña.Text.Equals(""))
{
MessageBox.Show("ingresar todos los campos");
}
else
{
if (!validarRut(txt_rut.Text))
{
System.Windows.Forms.MessageBox.Show("usuario o contraseña incorrecto");
txt_rut.Focus();
}
else
{
logear(this.txt_rut.Text, this.txt_contraseña.Text);
}
}
}
}
catch (Exception f)
{
MessageBox.Show(f.Message + f.StackTrace, "errror tipo 11");
}
}
public void logear(string rut, string pass)
{
try {
//con.Open();
MySqlCommand cmd = new MySqlCommand("select run_usuario, id_tipousuario, id_estado, id_session, password from tb_usuarios_adm where run_usuario = '"+rut+"' AND password= '"+pass+"'", conexion.obtenerConexion());
MySqlDataAdapter sda = new MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
// si lee
if(dt.Rows.Count == 1){
//identifica el tipo
if(dt.Rows[0][1].ToString() == "1")
{
//identifica el estado
if (dt.Rows[0][2].ToString() == "1")
{
//identifica la session
if(dt.Rows[0][3].ToString() == "1") {
//this.Hide();
//new administrador.Principal_administrador(dt.Rows[0][0].ToString()).Show();
if (dt.Rows[0][4].ToString() == "excsa")
{
new cambio_contraseña_inicio(dt.Rows[0][0].ToString()).ShowDialog();
}
else
{
this.Hide();
new administrador.Principal_administrador(dt.Rows[0][0].ToString()).ShowDialog();
}
}else{
DialogResult result = MessageBox.Show("desea cambiar de session", "usuario activo", MessageBoxButtons.YesNoCancel);
if (result == DialogResult.Yes)
{
}
else if (result == DialogResult.No)
{
MessageBox.Show("el otro usuario continua activo");
}
else if (result == DialogResult.Cancel)
{
}
}
}
else {
MessageBox.Show("estado bloqueado");
}
//identifica el tipo
}else if (dt.Rows[0][1].ToString() == "2")
{
//identifica el estado
if (dt.Rows[0][2].ToString() == "1")
{
//identifica la session
if(dt.Rows[0][3].ToString() == "1") {
Random rdn = new Random();
int a = rdn.Next(1000,9000);
loginprocedimiento procedimiento = new loginprocedimiento();
procedimiento.session(txt_rut.Text, a);
this.Hide();
new operaciones.principal_operaciones(dt.Rows[0][0].ToString(), a.ToString()).Show();
if (dt.Rows[0][4].ToString() == "excsa")
{
new cambio_contraseña_inicio(dt.Rows[0][0].ToString()).Show();
}
}
else
{
DialogResult result = MessageBox.Show("desea cambiar de session", "usuario activo", MessageBoxButtons.YesNoCancel);
if (result == DialogResult.Yes)
{
//se genera el random para obtener el numero aleatorio
Random rdn = new Random();
int a = rdn.Next(1000, 9000);
// atraves de procedimiento actualizo el campo session del usuario en la base de datos
loginprocedimiento procedimiento = new loginprocedimiento();
procedimiento.session(txt_rut.Text, a);
this.Hide();
// paso al siguiente formulario el rut y la variable generada
new
operaciones.principal_operaciones(dt.Rows[0][0].ToString(), a.ToString()).Show();
}
else if (result == DialogResult.No)
{
MessageBox.Show("la otra session continua activa");
}else if (result == DialogResult.Cancel){
}
}
}else{
MessageBox.Show("estado bloqueado");
}
}
}else{
MessageBox.Show("usuario y/o contraseña incorrecto");
}
}
catch (Exception f) {
//MessageBox.Show("ingrese caracteres validos");
}finally{
//con.Close();
conexion.obtenerConexion().Close();
}
}