Function to Check illegal characters for preventing Sql Injection Attack
With this codesnippet we can check illegal characters for preventing Sql injection attack
Please see below the code
/// <summary>
/// function to check illegal characters for preventing Sql Injection Attack.
/// </summary>
/// <param name="data">input data</param>
/// <returns>result as true if data has illegal character otherwise false</returns>
public bool IsPotentialSqlInjectionAttack(string data)
{
// Check to see whether the data contains illegal character
// or the string for making comment such as "--" or "/*"
char[] illegalChars = { ';', '\'', '\\', '"', '=', '%', '_', '*' };
if((data.IndexOfAny(illegalChars)!=-1) || data.Contains("--") || data.Contains("/*"))
{
return true;
}
else
{
return false;
}
}
Hope u like this