2011-12-15 205 views
0

我一直在谷歌上搜索現在有一陣子,我仍然無法找到答案,我的問題比較用戶輸入.. 是:我想知道如果有一個方法或方式來獲得數據庫的價值,然後比較它...我真的不知道如何解釋它..所以我想我會告訴你我的代碼到目前爲止。順便說一句使用netbean來製作這個程序,並使用odbc數據庫即時通訊(mircosoft訪問)。林還使用嘗試捕捉的代碼,但IDK的如何使其顯示..登錄 - 數據庫

下面的程序並沒有真正的工作,我希望它too..since我有比較問題的方式。 在此先感謝。

if(request.getParameter("username")!=null && request.getParameter("username") !="" 
     && request.getParameter("password")!=null && request.getParameter("password")!=""){ 

    String user = request.getParameter("username").toString(); 
    String pass = request.getParameter("password").toString(); 

    String check = "SELECT AccountType FROM Testing WHERE Username='"+user+"' AND Password ='"+pass+"'"; 
    rs = stmt.executeQuery(check); 
    String info = rs.getString(check); // trying to get the AccountType and store it into a string 

    while(rs.next()){ 
     if(info != null && info !=""){ //checks to see if the account exist in the database     
      if(info.equals("Admin")){ //checks to see if AccountType is "Admin" 
        response.sendRedirect("AdminConsole.jsp"); 
      }else 
       response.sendRedirect("UserConsole.jsp"); 
     }else 
      response.sendRedirect("ErrorPage2.jsp"); 
    } 
}else 
    response.sendRedirect("ErrorPage.jsp"); 

connection.close(); 

}

+2

您有一個SQL注入漏洞。 – SLaks 2011-12-15 19:06:17

+2

你不應該以純文本存儲密碼。 – SLaks 2011-12-15 19:06:39

回答

0

請,請不要這樣做。您不應該在代碼中執行SQL,也不應以純文本格式存儲密碼。你應該做的是調用一個參數化過程,該過程以用戶名/密碼作爲參數,然後返回角色(或任何你想要的)。密碼至少應該被散列。

像這樣:http://www.daniweb.com/software-development/csharp/threads/87556

在MS訪問的存儲過程被稱爲一個存儲的查詢: http://msdn.microsoft.com/en-us/library/aa140021(v=office.10).aspx

相關部分:

PROC:


create procedure ValidateUserLogin 
    @UserName varchar(30) 
    , @Password varchar(30) 
as 
begin 
    if exists (select * from UsersTable as ut 
    where ut.UserName = @UserName AND ut.Password = @Password) 
    select 1; 
    else 
    select 0; 
end 


private bool IsValidatedUser(string username, string password) { 
    try { 
    bool rv = false; 

    using (SqlConnection con = new SqlConnection(connectionString)) { 
     using (SqlCommand cmd = new SqlCommand()) { 
     con.Open(); 

     cmd.Connection = con; 
     cmd.CommandType = CommandType.StoredProcedure; 
     cmd.CommandText = "ValidateUserLogin"; 

     cmd.Parameters.Add("@UserName", SqlDbType.VarChar, 30).Value = username; 
     cmd.Parameters.Add("@Password", SqlDbType.VarChar, 30).Value = password; 

     rv = Convert.ToBoolean(cmd.ExecuteScalar()); 

     con.Close(); 
     } 
    } 

    return rv; 
    } 
catch (Exception ex) { 
    // Log errors 
    throw; 
    } 
}