Пример #1
0
        private void btnLogin_Click(object sender, EventArgs e)
        {
            this.btnLogin.BackgroundImage = new Bitmap(icons.buttonActive);
            string usrEmail = txtEmail.Text, usrPass = txtPass.Text;
            string sqlRetEmail = "", sqlRetPass = "";

            try
            {
                if (usrEmail == null || usrPass == null || usrEmail == "" || usrPass == "")
                {
                    //throw new Exception("No inputs detected.");
                }//End if

                List <SqlParameter> Parameters = new List <SqlParameter>();
                Parameters.Add(new SqlParameter("Email", usrEmail));
                Parameters.Add(new SqlParameter("Password", usrPass));
                var       sqlOut = DAL.ExecStoredProcedure("Login", Parameters);
                DataTable dt     = sqlOut;

                foreach (DataRow row in sqlOut.Rows)
                {
                    sqlRetEmail = row["Email"].ToString();
                    sqlRetPass  = row["Password"].ToString();
                }//End loop


                if (sqlRetEmail == usrEmail.ToString() && sqlRetPass == usrPass.ToString())
                {
                    if (chkRememberMe.Checked)
                    {
                        //Variables to store in Windows UAC for later user.
                        Properties.Settings.Default.UserName = usrEmail;
                        Properties.Settings.Default.Password = usrPass;
                        Properties.Settings.Default.Save();

                        //Prepare two pre-encrypted strings for later use.
                        string encryptedString = StringCipher.Encrypt(usrEmail, usrPass);
                        string encryptedPass   = StringCipher.Encrypt(usrPass, usrPass);

                        string[] saveToFileLines = { encryptedString, encryptedPass, StringCipher.Encrypt("preferred", usrPass) };

                        //Look for AppData\Roaming folder.
                        string fName = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Name.ToString() + "\\resources\\";

                        //MessageBox.Show(fName + usrEmail.ToString().Split('@')[0] + ".usr");      //Used for testing

                        //Determine if file and directory exists.
                        if (!File.Exists(@fName + usrEmail.ToString().Split('@')[0] + ".usr"))
                        {
                            //Does just the directory exist?
                            if (!Directory.Exists(@fName))
                            {
                                //Create if not.
                                Directory.CreateDirectory(@fName);
                            }//End If

                            //Prepare and deliver directory.
                            System.IO.File.WriteAllLines(fName + usrEmail.ToString().Split('@')[0] + ".usr", saveToFileLines);
                            System.IO.File.Encrypt(fName + usrEmail.ToString().Split('@')[0] + ".usr");
                        }//End If
                    }
                    LoggedIn clientForm = new LoggedIn();
                    clientForm.Show();
                    this.Hide();
                } //End If
                else
                {
                    lblInvLogin.Text    = "Invalid Login";
                    lblInvLogin.Visible = true;
                    if (x == 2)
                    {
                        lblInvLogin.Text = "Invalid Login; Access has been locked for 5 minutes.";
                        txtEmail.Enabled = false;
                        txtPass.Enabled  = false;
                        btnLogin.Enabled = false;
                        y = 0;
                        timer1.Enabled = true;
                        timer1.Start();
                        x = 0;
                    }
                    else
                    {
                        x++;
                    }
                }//End if
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.ToString());
            }
        }//End button click function