2011-12-15 207 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; 
    } 
}