Jua*_*uan 2 entity-framework sql-injection .net-core
我想使用 EF FromSqlInterpolated 或 FromSqlRaw 创建查询,以允许我使用 Like 子句,但我不知道在不打开应用程序遭受 SqlInjection 攻击的情况下执行此操作的正确方法是什么。第一种方法将我带到了以下代码
var results = _context.Categories.FromSqlInterpolated(
$"Select * from Category where name like {"%" + partialName + "%"}");
Run Code Online (Sandbox Code Playgroud)
第一个测试工作正常,它在提供预期字符串时返回结果,并且当我提供类似的内容时不返回任何内容';select * from Category Where name='Notes'--%';
仍然我对 SqlInjection 不太了解,至少不足以对之前显示的查询感到安全。有人知道查询是否安全,或者是否有正确的方法?谢谢
从这个文件
FromSqlInterpolated和ExecuteSqlInterpolated方法允许以防止 SQL 注入攻击的方式使用字符串插值语法。
var results = _context.Categories.FromSqlInterpolated(
$"Select * from Category where name like {"%" + partialName + "%"}");
Run Code Online (Sandbox Code Playgroud)
或者您也可以像这样将查询更改为 Linq-to-Entity
var results = _context.Categories.Where(p => p.name.Contains(partialName ));
Run Code Online (Sandbox Code Playgroud)