asp.net에서 SQL 인젝션을 피하는 가장 좋은 방법은 무엇입니까? 그러나 동시에 사용자에게 자유롭게 특별한 기호를 입력하고 싶습니다.asp.net에서 SQL 인젝션을 처리하는 방법
편집
나는 기업의 라이브러리 및 저장 프로 시저
asp.net에서 SQL 인젝션을 피하는 가장 좋은 방법은 무엇입니까? 그러나 동시에 사용자에게 자유롭게 특별한 기호를 입력하고 싶습니다.asp.net에서 SQL 인젝션을 처리하는 방법
편집
나는 기업의 라이브러리 및 저장 프로 시저
을 bobby-tables에서 언급 한대로 사용하십시오.
private static void SqlCommandPrepareEx(string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand command = new SqlCommand(null, connection);
// Create and prepare an SQL statement.
command.CommandText =
"INSERT INTO Region (RegionID, RegionDescription) " +
"VALUES (@id, @desc)";
SqlParameter idParam = new SqlParameter("@id", SqlDbType.Int, 0);
SqlParameter descParam =
new SqlParameter("@desc", SqlDbType.Text, 100);
idParam.Value = 20;
descParam.Value = "First Region";
command.Parameters.Add(idParam);
command.Parameters.Add(descParam);
// Call Prepare after setting the Commandtext and Parameters.
command.Prepare();
command.ExecuteNonQuery();
// Change parameter values and call ExecuteNonQuery.
command.Parameters[0].Value = 21;
command.Parameters[1].Value = "Second Region";
command.ExecuteNonQuery();
}
}
매개 변수화 된 쿼리를 사용하고, 어떤 parametrised 쿼리를 사용하고 있지 않다. 검색어가있는 곳이면 어디서든 사용할 수 있으며 사용자는 특히 느끼는 기호를 입력 할 수 있습니다. 당신이 ORM을 사용하는 경우
이 꽤 많이 당신을 위해 처리됩니다,하지만 당신이하지 않으면, 당신은해야 할 일이 같은 것입니다 : 그 쿼리가 100 %
comm.CommandText = "insert into MyTable (col1, col2) values (@col1, @col2)";
comm.Parameters.AddWithValue("@col1", 123);
comm.Parameters.AddWithValue("@col2", "; drop table whatever; --");
comm.ExecuteNonQuery();
입니다 광고 nauseum을 실행하는 것이 안전합니다. .NET에서 매개 변수를 처리하면 모든 설정이 완료됩니다.
또한 varchar
이 아닌 nvarchar
(유니 코드) 열을 사용해야합니다. 사용자가 ANSI 문자 집합 외부에 심볼을 삽입하려는 경우입니다.
+1하지만 저장 프로 시저를 사용하는 중에도 따라야 할 좋은 습관을 제안 할 수 있습니까? – Zerotoinfinity
스토어드 프로 시저를 호출하는 경우에는 별도로 설명한 것처럼'comm.Prepare()'를 사용하는 것이 좋지만 그 외에는 주입을 피하는 것이 가장 좋습니다. – Eric