Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
  2. {
  3. if (keyData == (Keys.Control | Keys.V))
  4. {
  5. errorProviderLogin.SetError(txtUsername, "Pasting is not allowed");
  6. MessageBox.Show("Pasting is not allowed...", "CTRL +V",
  7. MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  8. errorProviderLogin.Clear();
  9. return true;
  10. }
  11. else
  12. return base.ProcessCmdKey(ref msg, keyData);
  13. }
  14.  
  15.  
  16. private void frmLogin_Load(object sender, EventArgs e)
  17. {
  18.  
  19. txtUsername.Clear();
  20. txtPassword.Clear();
  21. }
  22.  
  23. private void txtUsername_KeyPress(object sender, KeyPressEventArgs e)
  24. {
  25. byte num = Convert.ToByte(e.KeyChar);
  26.  
  27. if(num == 13)
  28. {
  29. SendKeys.Send("{Tab}");
  30. e.Handled = true;
  31. }
  32. else if((num == 39) || (num == 34) || (num == 59))
  33. {
  34. e.Handled = true;
  35. }
  36.  
  37. }
  38.  
  39. private void txtPassword_KeyPress(object sender, KeyPressEventArgs e)
  40. {
  41. if (e.KeyChar.ToString() == "\r")
  42. {
  43. SendKeys.Send("{Tab}");
  44. e.Handled = true;
  45. }
  46. }
  47.  
  48. private void btnOkay_Click(object sender, EventArgs e)
  49. {
  50. //establish connection
  51. SqlConnection cn = new SqlConnection();
  52.  
  53. if (cn.State == ConnectionState.Closed)
  54. {
  55. cn.ConnectionString = "Data Source=" +
  56. stcSalesInv.myServer + ";Initial Catalog=" +
  57. stcSalesInv.myDataBase + ";Integrated Security=" +
  58. stcSalesInv.myIntSec + "; User ID =" + stcSalesInv.myUserID +
  59. "; Password =" + stcSalesInv.myPassword + ";";
  60. cn.Open();
  61. }
  62. SqlCommand cmd = new SqlCommand();
  63. SqlDataReader reader;
  64.  
  65. //validate
  66. if (txtUsername.Text == "")
  67. {
  68. MessageBox.Show("Please type your username.");
  69. txtUsername.Focus();
  70. return;
  71. }
  72. else if (txtPassword.Text == "")
  73. {
  74. MessageBox.Show("Please type your password.");
  75. txtPassword.Focus();
  76. return;
  77. }
  78. //SQL Injection
  79. if((Regex.IsMatch(txtUsername.Text, "[\'\"\\/*-;{}()]_")== true) ||
  80. (Regex.IsMatch(txtUsername.Text, "[xp_]")==true ))
  81. {
  82. MessageBox.Show("The Username contains characters that may override the system.",
  83. "SQL INJECTION", MessageBoxButtons.OK, MessageBoxIcon.Error);
  84. return;
  85. }
  86. //check username and password
  87. try
  88. {
  89. cmd.CommandType = CommandType.Text;
  90. //cmd.CommandText = "SELECT Username, UserLevel " +
  91. //"FROM Users2 WHERE Username '" + txtUsername.Text + "' AND Password = '" + EncDec.Encrypt(txtPassword.Text, "secret") +
  92. // "'";
  93.  
  94. cmd.Parameters.Add("@username", txtUsername.Text);
  95. cmd.Parameters.Add("@password", EncDec.Encrypt(txtPassword.Text, "secret"));
  96. cmd.CommandText = "SELECT Username, UserLevel " +
  97. " FROM Users2 WHERE Username = @username " +
  98. " AND Password = @password";
  99.  
  100. cmd.Connection = cn;
  101. reader = cmd.ExecuteReader();
  102.  
  103. if (reader.HasRows == false)
  104. {
  105. //stcSalesInv.ShowBar = false;
  106.  
  107. MessageBox.Show("Please use valid username and type correct passowrd");
  108. return;
  109. }
  110. stcSalesInv.ShowBar = true;
  111.  
  112. while (reader.Read())
  113. {
  114. stcSalesInv.UserName = reader.GetValue(0).ToString();
  115. stcSalesInv.UserLevel = reader.GetValue(1).ToString();
  116. }
  117.  
  118. cn.Close();
  119. reader.Close();
  120.  
  121. this.Hide();
  122.  
  123. stcSalesInv.NewMDI.Show();
  124. stcSalesInv.NewMDI.Focus();
  125. stcSalesInv.NewMDI.Refresh();
  126.  
  127. }
  128. catch (Exception errMsg)
  129. {
  130. MessageBox.Show(errMsg.Message);
  131. }
  132. }
  133.  
  134. private void btnCancel_Click(object sender, EventArgs e)
  135. {
  136. this.Close();
  137. }
  138.  
  139. private void txtUsername_MouseDown(object sender, MouseEventArgs e)
  140. {
  141. if(e.Button == MouseButtons.Right)
  142. {
  143. errorProviderLogin.SetError(txtUsername, "Right-click is not allowed");
  144. MessageBox.Show("Right-click is not allowed...", "MOUSE BUTTON",
  145. MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  146. errorProviderLogin.Clear();
  147. return;
  148. }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement