ASP.NET MVC限制同一個(gè)IP地址單位時(shí)間間隔內(nèi)的請求次數(shù)
有時(shí)候,當(dāng)用戶請求一個(gè)Controller下的Action,我們希望,在單位時(shí)間間隔內(nèi),比如每秒,每分鐘,每小時(shí),每天,每星期,限制同一個(gè)IP地址對某個(gè)Action的請求次數(shù)。如何做呢?
stefanprodan的MvcThrottle能很好地解決這個(gè)問題,以及其它類型的IP限制問題。在這里:https://github.com/stefanprodan/MvcThrottle
把項(xiàng)目從GitHub下載下來,在本地打開。
找到MvcThrottle類庫,打開ThrottlingFilter這個(gè)類,在該類的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ù)時(shí),跳轉(zhuǎn)到項(xiàng)目根目錄下的Error.html文件。
生成該類庫,類庫MvcThrottle.dll生成在類庫的bin/Debug文件夾下。
在ASP.NET MVC 4 下創(chuàng)建一個(gè)項(xiàng)目。
在項(xiàng)目根目錄下創(chuàng)建一個(gè)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}); } }
生成解決方案。
報(bào)錯(cuò)了!What Happened?
原來MvcThrottle是ASP.NET MVC 5下開發(fā)的。
有辦法。重新打開MvcThrottle項(xiàng)目的類庫,在引用中刪除原來的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> }
當(dāng)在單位時(shí)間間隔內(nèi)超過規(guī)定次數(shù),就彈出"請求次數(shù)過多"提示框。
在Home/Other.cshtml視圖中:
@{ ViewBag.Title = "Other"; Layout = "~/Views/Shared/_Layout.cshtml"; } <h2>Other</h2>
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
相關(guān)文章
如何利用擴(kuò)展方法來鏈?zhǔn)降膶VC 3中的頁面進(jìn)行驗(yàn)證
雖然擴(kuò)展方法只是改變了我們寫代碼的方式,但是如果我們使用得當(dāng),可以給我們帶來巨大的編碼效率的提升接下來介紹通過擴(kuò)展方法(鏈?zhǔn)椒椒ǎ镸VC 3視圖添加驗(yàn)證2013-01-01利用docker-compose搭建AspNetCore開發(fā)環(huán)境
這篇文章主要為大家詳細(xì)介紹了利用docker-compose搭建AspNetCore開發(fā)環(huán)境,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07Asp.Net 動態(tài)頁面轉(zhuǎn)靜態(tài)頁面主要代碼
關(guān)于在Asp.Net中動態(tài)頁面轉(zhuǎn)靜態(tài)頁面的方法網(wǎng)上比較多。結(jié)合實(shí)際的需求,我在網(wǎng)上找了一些源代碼,并作修改?,F(xiàn)在把修改后的代碼以及說明寫一下。2009-12-12讓GridView只顯示特定用戶的數(shù)據(jù)的方法
GridView 只顯示特定用戶的數(shù)據(jù)2008-10-10MVC+EasyUI+三層新聞網(wǎng)站建立 詳情頁面制作方法(八)
這篇文章主要為大家詳細(xì)介紹了MVC+EasyUI+三層新聞網(wǎng)站建立的第八篇,教大家如何制作詳情頁面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07在ASP.NET中用MSDNURLRewriting實(shí)現(xiàn)Url Rewriting
在ASP.NET中用MSDNURLRewriting實(shí)現(xiàn)Url Rewriting...2007-03-03asp.ent下合并兩個(gè)結(jié)構(gòu)相同的DataTable
今天遇到了一個(gè)情況,就是從一張數(shù)據(jù)表中讀取幾個(gè)符合條件1的客戶的信息,然后再讀取幾個(gè)符合條件2的客戶的信息,最后顯示出來.因?yàn)榍昂髢纱螖?shù)據(jù)的客戶信息的結(jié)構(gòu)是完全相同的,所以干脆合并成一個(gè)DataTable再賦值給GridView好了.2010-02-02