Home > Blockchain >  Is there a way that I can keep a user logged in throughout multiple forms after an initial login
Is there a way that I can keep a user logged in throughout multiple forms after an initial login

Time:03-20

I am fairly new to coding in C# and using windows forms applications and SQLite for databases, and I was wondering if there way that I can keep a user logged in and display said user's ID on different forms to show that they are logged in. The project itself is an ordering system and I wish for a customer to login to perform any purchases so that it can be logged as part of a management report. The code is below if it is of any help.

        int charlengthusername= usernametextbox.Text.Length;
        int charlengthpassword = passwordtextbox.Text.Length;
        if (charlengthusername <= 4 || charlengthusername >= 6 || charlengthpassword >=13||String.IsNullOrEmpty(usernametextbox.Text)||String.IsNullOrEmpty(passwordtextbox.Text))
        {
            MessageBox.Show("Details Entered Don't Meet The Expected Character Length Requirements. Try Again.");
            usernametextbox.Clear();
            passwordtextbox.Clear();
        }
        //make another if for user and pass matching  
        //change else below to be inside of username and password match if statement so that it will only send to custmainmenu when username and password has been matched
        else
        {
            sqlite_conn = new SQLiteConnection("Data Source=CustomerDatabase.db; Version = 3; New = True; Compress = True;");
            sqlite_conn.Open();
            SQLiteDataReader sqlite_datareader;
            string user = usernametextbox.Text;
            string pass = passwordtextbox.Text;
            sqlite_cmd = sqlite_conn.CreateCommand();
            sqlite_cmd.Connection = sqlite_conn;
            sqlite_cmd.CommandText = "SELECT * FROM TblCustomer where CustRandID='"   usernametextbox.Text   "' AND CustPassword='"   passwordtextbox.Text   "'";
            sqlite_datareader = sqlite_cmd.ExecuteReader();
            if (sqlite_datareader.Read())
            {

                MessageBox.Show("Login Successful! ");
                this.Hide();
                var openmainmenu = new CustomerMainMenu();
                openmainmenu.Closed  = (t, args) => this.Close();
                openmainmenu.Show();
            }
            else
            {
                MessageBox.Show("Incorrct Details! Try Again! ");
                this.Hide();
                var spenmainmenu = new CustomerLogin();
                spenmainmenu.Closed  = (s, args) => this.Close();
                spenmainmenu.Show();
            }
            sqlite_conn.Close();
        }
    }

CodePudding user response:

The simplest approach would be to just store the validated username/password in a public static property in a static class.

public static class AppInfo {
  public static string Username = "";
}

// ...

MessageBox.Show("Login Successful! ");
AppInfo.Username = user;

You can then access that Username (and any other global data your app might need) from AppInfo (which you could name something else, of course).

CodePudding user response:

You can use static members in a class and reach them from any form.

   class UserInfo {
        public static String UserName  ="";
        public static bool IsLoggedIn  = false;
    }

You need to set values inside of login successful block like that:

UserInfo.IsLoggedIn = true;
UserInfo.UserName = "Uday Patel";
  • Related