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

tee*_*emo 0 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
Run Code Online (Sandbox Code Playgroud)

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

Ans*_*ers 5

不要使用字符串连接来构建SQL查询.永远.您不仅会遇到这样的问题,还会使您容易受到SQL注入攻击.使用参数化查询(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
Run Code Online (Sandbox Code Playgroud)