在asp.net mvc下限制同一个ip地址单位时间间隔内的请求次数
有时候,当用户请求一个controller下的action,我们希望,在单位时间间隔内,比如每秒,每分钟,每小时,每天,每星期,限制同一个ip地址对某个action的请求次数。如何做呢?
stefanprodan的mvcthrottle能很好地解决这个问题,以及其它类型的ip限制问题。在这里:github - stefanprodan/mvcthrottle: asp.net mvc throttling filter
把项目从github下载下来,在本地打开。
找到mvcthrottle类库,打开throttlingfilter这个类,在该类的onactionexecuting方法中修改如下:
//check if limit is reached
if (ratelimit > 0 && throttlecounter.totalrequests > ratelimit)
{
//log blocked request
if (logger != null) logger.log(computelogentry(requestid, identity, throttlecounter, ratelimitperiod.tostring(), ratelimit, filtercontext.httpcontext.request));
//break execution and return 409
var message = string.isnullorempty(quotaexceededmessage) ?
"http request quota exceeded! maximum admitted {0} per {1}" : quotaexceededmessage;
//add status code and retry after x seconds to response
filtercontext.httpcontext.response.statuscode = (int)quotaexceededresponsecode;
filtercontext.httpcontext.response.headers.set("retry-after", retryafterfrom(throttlecounter.timestamp, ratelimitperiod));
filtercontext.result = quotaexceededresult(
filtercontext.requestcontext,
string.format(message, ratelimit, ratelimitperiod),
quotaexceededresponsecode,
requestid);
return;
}把以上替换成
//check if limit is reached
if (ratelimit > 0 && throttlecounter.totalrequests > ratelimit)
{
filtercontext.httpcontext.response.redirect("/error.html");
return;
} 让其在超过次数时,跳转到项目根目录下的error.html文件。
生成该类库,类库mvcthrottle.dll生成在类库的bin/debug文件夹下。
在asp.net mvc 4 下创建一个项目。
在项目根目录下创建一个library文件夹,把刚才的mvcthrottle.dll拷贝其中。
引用library文件夹下的mvcthrottle.dll组件。
在app_start文件夹中,修改filterconfig类如下:
public class filterconfig
{
public static void registerglobalfilters(globalfiltercollection filters)
{
var throttlefilter = new throttlingfilter
{
policy = new throttlepolicy(persecond: 1, perminute: 10, perhour: 60 * 10, perday: 600 * 10)
{
ipthrottling = true
},
repository = new cacherepository()
};
filters.add(throttlefilter);
}
}创建homecontroller,编写如下:
public class homecontroller : controller
{
public actionresult index()
{
return view();
}
[enablethrottling(persecond = 2, perminute = 5, perhour = 30, perday = 300)]
public actionresult other()
{
return view();
}
[httppost]
[enablethrottling(persecond = 2, perminute = 5, perhour = 30, perday = 300)]
public actionresult getsth()
{
return json(new {msg=true});
}
} 生成解决方案。
报错了!what happened?

原来mvcthrottle是asp.net mvc 5下开发的。
有办法。重新打开mvcthrottle项目的类库,在引用中删除原来的system.web.mvc,重新引用本地asp.net mvc4版本,重新引用本地的system.web.mvc。
重新生成类库,重新拷贝到library文件夹下,成功生成解决方案。
在home/index.cshtml视图中:
@{
viewbag.title = "index";
layout = "~/views/shared/_layout.cshtml";
}
<h2>index</h2>
<input type="button" id="btn" value="请求"/>
@section scripts
{
<script type="text/javascript">
$(function() {
$('#btn').on("click", function() {
$.post('@url.action("getsth")',function(data) {
if (data.msg) {
alert("请求成功一次");
} else {
alert("请求次数过多");
}
});
});
});
</script>
}当在单位时间间隔内超过规定次数,就弹出"请求次数过多"提示框。
在home/other.cshtml视图中:
@{
viewbag.title = "other";
layout = "~/views/shared/_layout.cshtml";
}
<h2>other</h2>当在单位时间间隔内超过规定次数,就跳转到预定的error.html页了。
到此这篇关于在asp.net mvc下限制同一个ip地址单位时间间隔内的请求次数的解决方法的文章就介绍到这了,更多相关asp.net mvc请求次数限制内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论