av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

ASP.NET MVC限制同一個IP地址單位時間間隔內的請求次數

瀏覽:137日期:2022-06-08 15:19:06

有時候,當用戶請求一個Controller下的Action,我們希望,在單位時間間隔內,比如每秒,每分鐘,每小時,每天,每星期,限制同一個IP地址對某個Action的請求次數。如何做呢?

stefanprodan的MvcThrottle能很好地解決這個問題,以及其它類型的IP限制問題。在這里:https://github.com/stefanprodan/MvcThrottle

把項目從GitHub下載下來,在本地打開。

找到MvcThrottle類庫,打開ThrottlingFilter這個類,在該類的OnActionExecuting方法中修改如下:

//check if limit is reachedif (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 reachedif (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>

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對的支持。如果你想了解更多相關內容請查看下面相關鏈接

標簽: ASP.NET
相關文章:
主站蜘蛛池模板: 人人爽日日躁夜夜躁尤物 | 欧美久久久久久久 | 欧美日韩久久 | 99热精品久久| 97久久国产| 亚洲一区免费在线 | 色嗨嗨 | 久久精品一区 | h片在线播放| 国产精品亚洲一区 | 91精品国产一区二区三区香蕉 | 7777奇米影视 | 91精品91久久久| 看a网站 | 国产91在线 | 亚洲 | 日韩欧美在线一区 | 亚洲精品久久久一区二区三区 | www狠狠爱com| 久久国产精品久久久久久 | 久久久久国产一区二区三区四区 | 精品久久久久一区 | 五月天国产在线 | wwww.xxxx免费 | 国产激情在线 | 中文av网站 | 99热最新网址 | 久久久女女女女999久久 | 日韩 欧美 二区 | 中文字幕亚洲视频 | 久久久久久国产精品免费 | www.久久久.com| 嫩草网 | 久久久国产一区二区三区 | 成人在线播放网站 | 国产观看 | 黄色精品 | 草久在线视频 | 毛片a级 | 久久精品a | 欧美性生活一区二区三区 | 97精品国产一区二区三区 |