2017-04-21 3 views
-5

내 코드의 일부가 SQL 인젝션을받습니다. 아래 코드는SQL 인젝션을 극복하는 방법

public int Insert(string usrtest) 

    { 
DataTable dt = new DataTable(); 
      SqlConnection con = new SqlConnection(conn); 
      // SqlCommand cmd = new SqlCommand("select * from table where [email protected]", con); 
      SqlDataAdapter adp = new SqlDataAdapter("select * from table where [email protected]", con); 
      con.Open(); 
      adp.SelectCommand.Parameters.AddWithValue("@name", usrtest); 

      adp.Fill(dt); 
      SqlCommand cmd1 = new SqlCommand("Update table set Date='" + DateTime.Now + "' where name='" + usrtest + "'", con); 

      cmd1.ExecuteNonQuery(); 
      con.Close(); 
} 
+0

두 코드의 차이점을 모르시겠습니까? 먼저 매개 변수화 된 쿼리를 적절히 사용하면 모든 것을 무시하고 문자열 연결을 통해 SQL을 다시 작성합니다. 또한 두 번째 쿼리에서 매개 변수를 사용하면 작업이 완료됩니다. – CodeCaster

+0

또한 SqlConnection, SqlCommand 및 SqlDataAdapter에 "using"문을 사용해야합니다. https://www.dotnetperls.com/sqlconnection 및 http://stackoverflow.com/questions/18205560/do-i-need- to-explicit-dispose-sqldataadapter입니다. – Polyfun

+0

편집을 가능하게하는 gridview에 대한 itemtemplate 텍스트 박스 컨트롤이 있습니다. 이것은 코드입니다 이 사이트에 크로스 사이트가 있습니까? (예 : "% 스크립트 위험? – Aswini

답변

0

문제는 당신이 문자열 연결을 사용하는 경우, 다음 명령에 있습니다

당신은 이미 당신이 Parameters를 사용 이전과했던 것처럼 위의 명령은 트레드해야
SqlCommand cmd1 = new SqlCommand("Update Usrtable set password_change_status=1, Date='" + DateTime.Now + "' where Uname='" + txtusr + "'", con); 

.

var cmd1 = new SqlCommand("Update Usrtable set password_change_status=1, [email protected] where [email protected]", con); 
cmd1.Parameters.AddWithValue("@Date",DateTime.Now); 
cmd1.Parameters.AddWithValue("@Uname",txtusr); 
2

여러분은 이미 코드에서 4 행을 수행했기 때문에 이미 바인드 매개 변수를 사용하는 방법을 알고있는 것처럼 보입니다. 두 번째 성명서에도 사용하십시오.

관련 문제