Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.60 KB | None | 0 0
  1. protected void loginbutton_Click(object sender, EventArgs e)
  2. {
  3. string UsernameRegex = "[a-zA-Z]+";
  4. string PasswordRegex = "[a-zA-Z0-9]+";
  5.  
  6. if (!Regex.IsMatch(usernametextbox.Text, UsernameRegex))
  7. {
  8. string UsernameCheck = "valid";
  9. }
  10. else
  11. {
  12. string UsernameCheck = "invalid";
  13. }
  14.  
  15. if (!Regex.IsMatch(passwordtextbox.Text, PasswordRegex))
  16. {
  17. string PasswordCheck = "valid";
  18. }
  19. else
  20. {
  21. string PasswordCheck = "invalid";
  22. }
  23.  
  24.  
  25. if(UsernameCheck = "valid") //i will include password here after i solved the problem
  26. {
  27. //do something
  28. }
  29. SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
  30. conn.Open();
  31. string checkuser = "select count(*) from Users where Username = @username and Password = @password";
  32.  
  33. SqlCommand com = new SqlCommand(checkuser, conn);
  34. com.Parameters.Add("@username", SqlDbType.NVarChar).Value = usernametextbox.Text;
  35. com.Parameters.Add("@password", SqlDbType.NVarChar).Value = passwordtextbox.Text;
  36.  
  37. int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
  38.  
  39. if (temp > 0)
  40. {
  41. Response.Redirect("Cars.aspx");
  42. }
  43. else
  44. {
  45. loginfaillabel.Text = "Your Username or Password doesn't match our records";
  46. }
  47. }
  48.  
  49. protected void loginbutton_Click(object sender, EventArgs e)
  50. {
  51. string UsernameRegex = "[a-zA-Z]+";
  52. string PasswordRegex = "[a-zA-Z0-9]+";
  53.  
  54. boolean isUsernameValid = Regex.IsMatch(usernametextbox.Text, UsernameRegex)
  55. boolean isPasswordValid = Regex.IsMatch(passwordtextbox.Text, PasswordRegex);
  56.  
  57.  
  58. if(!isUsernameValid || !isPasswordValid) //i will include password here after i solved the problem
  59. {
  60. //do something
  61. }
  62. else
  63. {
  64. const string checkuser = "SELECT 1 FROM Users WHERE Username = @username and Password = @password";
  65.  
  66. using(SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
  67. using(SqlCommand com = new SqlCommand(checkuser, conn))
  68. {
  69. conn.Open();
  70.  
  71. com.Parameters.Add("@username", SqlDbType.NVarChar).Value = usernametextbox.Text;
  72. com.Parameters.Add("@password", SqlDbType.NVarChar).Value = passwordtextbox.Text;
  73.  
  74. object temp = com.ExecuteScalar();
  75.  
  76. // I do not remember if it is null or System.DbNull.Value that is returned if nothing is returned
  77. // you will have to test it
  78. var didUserMatch = temp == null || temp == System.DbNull.Value ? false : true;
  79.  
  80. if (didUserMatch)
  81. {
  82. Response.Redirect("Cars.aspx");
  83. }
  84. else
  85. {
  86. loginfaillabel.Text = "Your Username or Password doesn't match our records";
  87. }
  88. }
  89. }
  90. }
  91.  
  92. bool UsernameCheck = false; // better name for this is isUsernameValie
  93.  
  94. if (!Regex.IsMatch(usernametextbox.Text, UsernameRegex))
  95. {
  96. UsernameCheck = true;
  97. }
  98. else
  99. {
  100. UsernameCheck = false;
  101. }
  102.  
  103. bool PasswordCheck = false;// better name for this is isPasswordValid
  104. if (!Regex.IsMatch(passwordtextbox.Text, PasswordRegex))
  105. {
  106. PasswordCheck = true;
  107. }
  108. else
  109. {
  110. PasswordCheck = false;
  111. }
  112.  
  113.  
  114. if (UsernameCheck == true) //i will include password here after i solved the problem
  115. {
  116. //do something
  117. }
  118. SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
  119. conn.Open();
  120. string checkuser = "select count(*) from Users where Username = @username and Password = @password";
  121.  
  122. SqlCommand com = new SqlCommand(checkuser, conn);
  123. com.Parameters.Add("@username", SqlDbType.NVarChar).Value = usernametextbox.Text;
  124. com.Parameters.Add("@password", SqlDbType.NVarChar).Value = passwordtextbox.Text;
  125.  
  126. int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
  127.  
  128. if (temp > 0)
  129. {
  130. Response.Redirect("Cars.aspx");
  131. }
  132. else
  133. {
  134. loginfaillabel.Text = "Your Username or Password doesn't match our records";
  135. }
  136. }
  137.  
  138. protected void loginbutton_Click(object sender, EventArgs e)
  139. {
  140. string UsernameRegex = "[a-zA-Z]+";
  141. string PasswordRegex = "[a-zA-Z0-9]+";
  142.  
  143. var userName = usernametextbox.Text;
  144. var password = passwordtextbox.Text;
  145.  
  146. if (!Regex.IsMatch(userName, UsernameRegex))
  147. {
  148. // do something
  149. return; // There is no need to go on
  150. }
  151.  
  152. if(!Regex.IsMatch(password, PasswordRegex))
  153. {
  154. // do something
  155. return; // There is no need to go on
  156. }
  157.  
  158. //If we can come here, we can go DB
  159.  
  160. // To be dispose when the job is done
  161. using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
  162. {
  163.  
  164. try
  165. {
  166. // To be dispose when the job is done
  167. using (SqlCommand com = new SqlCommand(checkuser, conn))
  168. {
  169. conn.Open();
  170. string checkuser = "select count(*) from Users where Username = @username and Password = @password";
  171. com.Parameters.Add("@username", SqlDbType.NVarChar).Value = userName;
  172. com.Parameters.Add("@password", SqlDbType.NVarChar).Value = password;
  173. int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
  174. if (temp > 0)
  175. {
  176. Response.Redirect("Cars.aspx");
  177. }
  178. else
  179. {
  180. loginfaillabel.Text = "Your Username or Password doesn't match our records";
  181. }
  182. }
  183. }
  184. catch (Exception ex)
  185. {
  186.  
  187. // you can handle error. maybe logs
  188. }
  189. }
  190. }
  191.  
  192. <p>Username (Alphabetic only, no spaces):<br />
  193. <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
  194. <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" Display="Dynamic" ErrorMessage="Username is required"></asp:RequiredFieldValidator>
  195. <asp:RegularExpressionValidator ID="NameValidator" runat="server" ControlToValidate="TextBox1" Display="Dynamic" ErrorMessage="Invalid - Alaphabetic only" ValidationExpression="[a-zA-Z]+" EnableClientScript="True"></asp:RegularExpressionValidator>
  196. </p>
  197. <p>Password (Alphanumeric only, no spaces):<br />
  198. <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
  199. <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TextBox2" Display="Dynamic" ErrorMessage="Password is required"></asp:RequiredFieldValidator>
  200. <asp:RegularExpressionValidator ID="PwdValidator" runat="server" ControlToValidate="TextBox2" Display="Dynamic" ErrorMessage="Invalid -Alphanumeric Only" ValidationExpression="[w]+" EnableClientScript="True"></asp:RegularExpressionValidator>
  201. </p>
  202. <p>
  203. <asp:Button ID="Button1" runat="server" OnClick="BtnSubmit" Text="Login" />
  204. </p>
  205.  
  206. public partial class foo: Page
  207. {
  208. protected void Page_Load(object sender, EventArgs e)
  209. {
  210.  
  211. }
  212.  
  213. protected void BtnSubmit(object sender, EventArgs e)
  214. {
  215. if (Page.IsValid)
  216. {
  217. //Do what you need to do only if IsValid which is the server-side validation check
  218. }
  219. }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement