在ASP.NET?MVC下限制同一個IP地址單位時間間隔內(nèi)的請求次數(shù)的解決方法
在ASP.NET MVC下限制同一個IP地址單位時間間隔內(nèi)的請求次數(shù)
有時候,當用戶請求一個Controller下的Action,我們希望,在單位時間間隔內(nèi),比如每秒,每分鐘,每小時,每天,每星期,限制同一個IP地址對某個Action的請求次數(shù)。如何做呢?
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;
} 讓其在超過次數(shù)時,跳轉(zhuǎn)到項目根目錄下的Error.html文件。
生成該類庫,類庫MvcThrottle.dll生成在類庫的bin/Debug文件夾下。
在ASP.NET MVC 4 下創(chuàng)建一個項目。
在項目根目錄下創(chuàng)建一個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);
}
}創(chuàng)建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下開發(fā)的。
有辦法。重新打開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("請求次數(shù)過多");
}
});
});
});
</script>
}當在單位時間間隔內(nèi)超過規(guī)定次數(shù),就彈出"請求次數(shù)過多"提示框。
在Home/Other.cshtml視圖中:
@{
ViewBag.Title = "Other";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Other</h2>當在單位時間間隔內(nèi)超過規(guī)定次數(shù),就跳轉(zhuǎn)到預定的Error.html頁了。
到此這篇關(guān)于在ASP.NET MVC下限制同一個IP地址單位時間間隔內(nèi)的請求次數(shù)的解決方法的文章就介紹到這了,更多相關(guān)ASP.NET MVC請求次數(shù)限制內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Asp.net web.config customErrors 如何設(shè)置
這篇文章主要介紹了詳解Asp.net web.config customErrors 如何設(shè)置,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02
Asp.net后臺把腳本樣式輸出到head標簽中節(jié)省代碼冗余
最近在學習開發(fā)服務(wù)器控件,其它就少不了為控件注冊js和css之類的資源文件,或者直接注冊純腳本樣式。其中就遇到如下問題 1、 注冊的資源文件或純腳本樣式在生成的頁面中都不在head標簽中(當然這個不影響頁面功能) 2、 一個頁面使用多個一樣的控件時,會出現(xiàn)重復輸入(出現(xiàn)多余代碼)2013-02-02
Community Server專題二:體系結(jié)構(gòu)
Community Server專題二:體系結(jié)構(gòu)...2007-03-03
Asp.Net Core Identity 隱私數(shù)據(jù)保護的實現(xiàn)
這篇文章主要介紹了Asp.Net Core Identity 隱私數(shù)據(jù)保護的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-01-01
.NET Core實現(xiàn)RabbitMQ消息隊列的示例代碼
RabbitMQ支持AMQP協(xié)議,可以通過多種方式與應(yīng)用程序交互,本文主要介紹了.NET Core實現(xiàn)RabbitMQ消息隊列的示例代碼,實現(xiàn)在生產(chǎn)者端發(fā)送消息,消費者端接收消息,并確保消息的可靠性2025-05-05
詳解.net core webapi 前后端開發(fā)分離后的配置和部署
這篇文章主要介紹了.net core webapi 前后端開發(fā)分離后的配置和部署,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-04-04

