(1)获取post的body参数
// 执行action前的过滤器方法
public void OnActionExecuting(ActionExecutingContext context)
{
// 获取request对象
HttpRequest request = context.HttpContext.Request;
//-- 由于mvc里已经读过request.Body,现在它的position在末尾
// 允许重新读body
request.EnableRewind();
// 将position置到开始位置
equest.Body.Position = 0;
// 读取body的数据流,并转为string
var reader = new StreamReader(request.Body);
var contentFromBody = reader.ReadToEnd();
}
(2)sql字符串验证
// 检测输入参数,防止sql注入
public static bool isParamOk(string InText)
{
if (InText == null)
return true;
// 数据库关键单词
string word = "and|exec|insert|select|delete|update|chr|mid|master|or|truncate|char|declare|join";
foreach (string i in word.Split('|'))
{
if ((InText.ToLower().IndexOf(i + " ") > -1) || (InText.ToLower().IndexOf(" " + i) > -1))
{
return false;
}
}
// 数据库关键字符
string singleChar = "=";
foreach (string i in singleChar.Split('|'))
{
if ((InText.ToLower().IndexOf(i) > -1))
{
return false;
}
}
return true;
}
(3)获取request的query参数
HttpRequest request = context.HttpContext.Request;
bool isParamOk = true;
foreach(var i in request.Query)
{
if (!isParamOk(i.Value))
{
isParamOk = false;
}
}