Beispiel #1
0
        private void btn_enter_Click(object sender, EventArgs e)
        {
            automation_systemEntities database = new automation_systemEntities(PublicVariable.MainConnectionString);

            try
            {
                if (txt_username.Text.Trim() != "" && txt_password.Text.Trim() != "")
                {
                    /////////////hashing password
                    SHA256CryptoServiceProvider SHA256 = new SHA256CryptoServiceProvider();
                    Byte[] B1;
                    Byte[] B2;
                    B1 = UTF8Encoding.UTF8.GetBytes(txt_password.Text.Trim());
                    B2 = SHA256.ComputeHash(B1);
                    string HashedPassword = BitConverter.ToString(B2);


                    var login_query = (from U in database.Users
                                       where U.Username == txt_username.Text.Trim()
                                       where U.Password == HashedPassword
                                       where U.Activity == 1
                                       select U).ToList();
                    if (login_query.Count == 1)
                    {
                        /// Obtain user profile for use throughout the app
                        PublicVariable.gUserFirstName  = login_query[0].UserFirstName;
                        PublicVariable.gUserFamilyName = login_query[0].UserFamily;
                        PublicVariable.gUserId         = login_query[0].userID;

                        /////Register user profile in log file to control entry and exit s.22
                        string computerName = System.Environment.MachineName;

                        UserLog UL = new UserLog();
                        UL.ComputerName  = computerName;
                        UL.IpAddress     = lbl_IP.Text.Trim();
                        UL.EnterDateTime = lbl_date.Text.Trim() + "-" + string.Format("{0:HH:mm:ss}", Convert.ToDateTime(DateTime.Now.Hour + ":" + DateTime.Now.Minute + ":" + DateTime.Now.Second));
                        UL.UserId        = PublicVariable.gUserId;

                        database.UserLogs.Add(UL);
                        database.SaveChanges();
                    }
                    else
                    {
                        MessageBox.Show("The user was not found");
                        return;
                    }
                    if (rdb_admin.Checked)
                    {
                        if (txt_username.Text.Trim() == "admin")
                        {
                            PublicVariable.gSetUser = 1; ////admin
                        }
                        else
                        {
                            MessageBox.Show("The user does not have administrator access");
                            return;
                        }
                    }
                    else
                    {
                        PublicVariable.gSetUser = 2; ///users
                    }

                    this.Close();
                }
            }
            catch
            {
                MessageBox.Show("There is a problem with the server, please try again");
            }
        }