I am trying to build a website vulnerable to SQL injections (for educational purposes) with an ASP.NET API. To do that, I would like to call FromSql
with a previously prepared SQL query as such:
String query = "SELECT * FROM users WHERE email = '"+email
+"' AND password = '"+password+"'";
return RepositoryContext.Users.FromSql(query).FirstOrDefault();
But this code will not compile as FromSql
is expecting a FormattableString
, and not a String
. I get the following error message:
Error CS1503 Argument 2: cannot convert from 'string' to 'System.FormattableString'
The following would compile, but then this code would not be vulnerable to SQL injections anymore as Entity Framework will perform a prepared SQL query and convert email and password to SQL query parameters :
return RepositoryContext.Users.FromSql(
$"SELECT * FROM users WHERE email = {email}
AND password = {password}").FirstOrDefault();
Is there a way to convert a String
to a FormattableString
?
Thank you for your help.