Guest User

Untitled

a guest
Aug 10th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. Login - Comparing user input with database
  2. if(request.getParameter("username")!=null && request.getParameter("username") !=""
  3. && request.getParameter("password")!=null && request.getParameter("password")!=""){
  4.  
  5. String user = request.getParameter("username").toString();
  6. String pass = request.getParameter("password").toString();
  7.  
  8. String check = "SELECT AccountType FROM Testing WHERE Username='"+user+"' AND Password ='"+pass+"'";
  9. rs = stmt.executeQuery(check);
  10. String info = rs.getString(check); // trying to get the AccountType and store it into a string
  11.  
  12. while(rs.next()){
  13. if(info != null && info !=""){ //checks to see if the account exist in the database
  14. if(info.equals("Admin")){ //checks to see if AccountType is "Admin"
  15. response.sendRedirect("AdminConsole.jsp");
  16. }else
  17. response.sendRedirect("UserConsole.jsp");
  18. }else
  19. response.sendRedirect("ErrorPage2.jsp");
  20. }
  21. }else
  22. response.sendRedirect("ErrorPage.jsp");
  23.  
  24. connection.close();
  25.  
  26. create procedure ValidateUserLogin
  27. @UserName varchar(30)
  28. , @Password varchar(30)
  29. as
  30. begin
  31. if exists (select * from UsersTable as ut
  32. where ut.UserName = @UserName AND ut.Password = @Password)
  33. select 1;
  34. else
  35. select 0;
  36. end
  37.  
  38. private bool IsValidatedUser( string username, string password ) {
  39. try {
  40. bool rv = false;
  41.  
  42. using ( SqlConnection con = new SqlConnection( connectionString ) ) {
  43. using ( SqlCommand cmd = new SqlCommand() ) {
  44. con.Open();
  45.  
  46. cmd.Connection = con;
  47. cmd.CommandType = CommandType.StoredProcedure;
  48. cmd.CommandText = "ValidateUserLogin";
  49.  
  50. cmd.Parameters.Add( "@UserName", SqlDbType.VarChar, 30 ).Value = username;
  51. cmd.Parameters.Add( "@Password", SqlDbType.VarChar, 30 ).Value = password;
  52.  
  53. rv = Convert.ToBoolean( cmd.ExecuteScalar() );
  54.  
  55. con.Close();
  56. }
  57. }
  58.  
  59. return rv;
  60. }
  61. catch ( Exception ex ) {
  62. // Log errors
  63. throw;
  64. }
  65. }
Add Comment
Please, Sign In to add comment