色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

ASP.NET MVC限制同一個(gè)IP地址單位時(shí)間間隔內(nèi)的請求次數(shù)

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

有時(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 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;}  

讓其在超過次數(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)鏈接

標(biāo)簽: ASP.NET
相關(guān)文章:
主站蜘蛛池模板: 成年女人免费观看 | 国产成人一区二区三区视频免费 | 黄色三级在线播放 | 久久精品视频播放 | 亚洲精品久久99久久 | 国产成人免费高清激情视频 | 国产观看在线 | 黄色a毛片 | 国产精品久久久久久久久久直 | 欧美人成在线观看ccc36 | 69成人| 久久―日本道色综合久久 | 国产三级a三级三级三级 | japanesehd国产在线无毒不卡 | 久草视频精品 | 国产色啪午夜免费视频 | 一区二区三区高清视频在线观看 | 国产a视频 | 欧美性色生活片免费播放 | 日韩视频在线观看一区二区 | 一级毛片视频免费观看 | 欧美成人一级视频 | 日韩一级欧美一级 | 三级网站视频在线观看 | 亚洲国产高清人在线 | 国产一区国产二区国产三区 | 亚洲精品一区二区三区福利 | 久久国产精品免费一区二区三区 | 久久88香港三级台湾三级中文 | 亚洲天堂久久久 | 自拍视频网 | 韩国一级性生活片 | 成 人 动漫在线观看网站网站 | 成人一级大片 | 88av在线视频 | 日韩在线视频一区二区三区 | 国产日韩欧美一区二区三区在线 | 中国一级做a爰片久久毛片 中日韩欧美一级毛片 | 黄色影视频 | 亚洲国产精品一区二区首页 | 九九视频高清视频免费观看 |