带有 Like 子句的 EF Core 原始查询
EF Core raw query with Like clause
我想使用允许我使用 Like 子句的 EF FromSqlInterpolated 或 FromSqlRaw 创建查询,但我不知道在不打开应用程序以进行 SqlInjection 攻击的情况下执行此操作的正确方法是什么。
第一种方法将我带到了以下代码
var results = _context.Categories.FromSqlInterpolated(
$"Select * from Category where name like {"%" + partialName + "%"}");
第一次测试工作正常,它 returns 在提供预期字符串时产生结果,而在我提供 ';select * from Category Where name='Notes'--%';
之类的东西时 returns 没有结果
我仍然对 SqlInjection 了解不多,至少不足以让我对之前显示的查询感到安全。
有人知道查询是否安全,或者是否有正确的方法吗?
谢谢
The FromSqlInterpolated and ExecuteSqlInterpolated methods allow using
string interpolation syntax in a way that protects against SQL injection attacks.
var results = _context.Categories.FromSqlInterpolated(
$"Select * from Category where name like {"%" + partialName + "%"}");
或者您也可以像这样将查询更改为 Linq-to-Entity
var results = _context.Categories.Where(p => p.name.Contains(partialName ));
我想使用允许我使用 Like 子句的 EF FromSqlInterpolated 或 FromSqlRaw 创建查询,但我不知道在不打开应用程序以进行 SqlInjection 攻击的情况下执行此操作的正确方法是什么。 第一种方法将我带到了以下代码
var results = _context.Categories.FromSqlInterpolated(
$"Select * from Category where name like {"%" + partialName + "%"}");
第一次测试工作正常,它 returns 在提供预期字符串时产生结果,而在我提供 ';select * from Category Where name='Notes'--%';
之类的东西时 returns 没有结果
我仍然对 SqlInjection 了解不多,至少不足以让我对之前显示的查询感到安全。
有人知道查询是否安全,或者是否有正确的方法吗?
谢谢
The FromSqlInterpolated and ExecuteSqlInterpolated methods allow using string interpolation syntax in a way that protects against SQL injection attacks.
var results = _context.Categories.FromSqlInterpolated(
$"Select * from Category where name like {"%" + partialName + "%"}");
或者您也可以像这样将查询更改为 Linq-to-Entity
var results = _context.Categories.Where(p => p.name.Contains(partialName ));