简体   繁体   中英

C# WPF Login then reuse Login Credentials to the next window if successfully logged-in

So let us say I have a log-in page. I am trying to log-in. If its user credentials is correct and it exists in the database then it will allow the user to log-in but I want to reuse that correct credentials to my new window. Example in the new window there is a textblock indicating the current user and its current role. how do I do that? should I make a new table called sessions and if the login is successful then store the credentials to the sessions_tbl then get the latest info in the session but idk how to that, can someone help me with the best approach, sorry if I'm dumb, just started c# 2 weeks ago..

here are my codes for logging in.

    private void LoggingIn()
    {
        SqlConnection sqlCon = new SqlConnection(@"Data Source=localhost\SQLEXPRESS; Initial Catalog=SalesDB; Integrated Security=true;"); // this is the connection to the database
        try
        {
            if (sqlCon.State == ConnectionState.Closed)
            {
                sqlCon.Open();
            }
            String query = "SELECT COUNT(1) FROM users_tbl where Username=@Username AND Password=@Password";
            SqlCommand sqlCmd = new SqlCommand(query, sqlCon);
            sqlCmd.CommandType = CommandType.Text;
            sqlCmd.Parameters.AddWithValue("@Username", txtUsername.Text);
            sqlCmd.Parameters.AddWithValue("@Password", txtPassword.Password);
            int count = Convert.ToInt32(sqlCmd.ExecuteScalar()); //if true the value will be converted to "1" in integer.

            if (count == 1)
            {
                MainWindow dashboard = new MainWindow();
                dashboard.Show();
                this.Close();
            }
            else
            {
                MessageBox.Show("Username or Password is incorrect. ");
                txtUsername.Focus();
                txtUsername.Clear();
                txtPassword.Clear();
            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            sqlCon.Close();
        }
    }

    //login button codes
    private void btnSubmit_Click(object sender, RoutedEventArgs e)
    {
        LoggingIn();
    }

=====================

    private void LoggingIn()
    {
        SqlConnection sqlCon = new SqlConnection(@"Data Source=localhost\SQLEXPRESS; Initial Catalog=SalesDB; Integrated Security=true;"); // this is the connection to the database
        try
        {
            if (sqlCon.State == ConnectionState.Closed)
            {
                sqlCon.Open();
            }
            String query = "SELECT COUNT(1) FROM users_tbl where Username=@Username AND Password=@Password";
            SqlCommand sqlCmd = new SqlCommand(query, sqlCon);
            sqlCmd.CommandType = CommandType.Text;
            sqlCmd.Parameters.AddWithValue("@Username", txtUsername.Text);
            sqlCmd.Parameters.AddWithValue("@Password", txtPassword.Password);
            int count = Convert.ToInt32(sqlCmd.ExecuteScalar()); //if true the value will be converted to "1" in integer.

            if (count == 1)
            {
                LoginInfo userInfo = new LoginInfo();

                using (SqlDataReader oReader = sqlCmd.ExecuteReader())
                {
                    while (oReader.Read())
                    {
                        userInfo.UserName = oReader["Username"].ToString();
                        userInfo.PassWord = oReader["Password"].ToString();
                        userInfo.Role = oReader["Role"].ToString();
                        userInfo.FirstName = oReader["First_Name"].ToString();
                        userInfo.LastName = oReader["Last_Name"].ToString();
                    }
                }

                MainWindow dashboard = new MainWindow();
                dashboard.Show();
                this.Close();
            }

Please Don't implement Login like that! It's considered a bad practice and a huge security vulnerability

You have the option to implement your own authentication and authorization with WPF.

The .NET Framework uses the System.Security.Principal.IIdentity and System.Security.Principal.IPrincipal interfaces as the basis for authentication and authorization and by implementing these fairly simple interfaces you can apply your own custom authentication in your applications.

Check These out:

Custom authorization in WPF

WPF: Implementing Custom Authentication And Authorization (MS)

You can pass to the next window as a parameter,

 MainWindow dashboard = new MainWindow(LoginInfo loginDetails);

Where LoginInfo is a class which holds your necessary details

public class LoginInfo{
   //add username
   // whatever
}

and on the first window assign the values to the object as,

LoginInfo userInfo = new LoginInfo();
userInfo.username = 'test';

then you can pass to the next window

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM