VBScript代码打破包含'的字符串'

时间:2013-09-04 16:04:47

标签: vbscript

我的一个用户有一个'在用户名内 我认为这打破了登录代码 在线上 tempPassword =的Request.Form(的 “userpassword”)

if (Request.Form("Action") = "Login") then
    tempUsername=Request.Form("UserName")
    tempPassword=Request.Form("UserPassword")

    if not (tempUserName = "") and not (tempPassword = "") then
        strSQL="SELECT ContactID,Email,Password from Directory WHERE Email='" & tempUsername & "' AND Password='" & tempPassword & "'"
        set rsQuery=cn.execute(strSQL)
        if not (rsQuery.EOF) then
            '-- Login correct, so set session variables
            Session("CBWorkUser") = "Yes"
            Session("CBWorkUserName") = rsQuery("Email")
            Session("CBWorkID") = rsQuery("ContactID")
        end if
        set rsQuery = nothing
    end if
end if

有什么解决方案可以解决这个问题?

1 个答案:

答案 0 :(得分:5)

不要使用字符串连接来构建SQL查询。永远。您不仅会遇到这样的问题,还会让您容易受到SQL injection的攻击。请改用parameterized queries(AKA准备好的陈述):

Set cmd  = CreateObject("ADODB.Command")
cmd.ActiveConnection = cn

Set p1 = cmd.CreateParameter("@email" , 200, 1, 255, tempUsername)
cmd.Parameters.Append p1
Set p2 = cmd.CreateParameter("@password" , 200, 1, 255, tempPassword)
cmd.Parameters.Append p2

cmd.CommandText = "SELECT ContactID,Email,Password FROM Directory " _
  & "WHERE Email=? AND Password=?"

Set rsQuery = cmd.Execute
相关问题