搜索

查看: 3076|回复: 11

[ASP.NET] ASP.NET MVC限制同一个IP地址单位时间间隔内的请求次数

[复制链接]
发表于 2023-5-4 11:32:32 | 显示全部楼层 |阅读模式
Editor 2023-5-4 11:32:32 3076 11 看全部
有时候,当用户请求一个Controller下的Action,我们希望,在单位时间间隔内,比如每秒,每分钟,每小时,每天,每星期,限制同一个IP地址对某个Action的请求次数。如何做呢?
stefanprodan的MvcThrottle能很好地解决这个问题,以及其它类型的IP限制问题。在这里:https://github.com/stefanprodan/MvcThrottle
把项目从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?

20221022101115985.png

20221022101115985.png


原来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";
}
Index

@section scripts
{
   
}
当在单位时间间隔内超过规定次数,就弹出"请求次数过多"提示框。
在Home/Other.cshtml视图中:
@{
    ViewBag.Title = "Other";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
Other
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对知鸟论坛的支持。如果你想了解更多相关内容请查看下面相关链接
回复

使用道具 举报

发表于 2023-6-28 20:06:30 | 显示全部楼层
胡37 2023-6-28 20:06:30 看全部
楼主太厉害了!楼主,I*老*虎*U!我觉得知鸟论坛真是个好地方!
回复

使用道具 举报

发表于 2023-6-29 07:08:25 | 显示全部楼层
音乐之家1 2023-6-29 07:08:25 看全部
感谢楼主的无私分享!要想知鸟论坛好 就靠你我他
回复

使用道具 举报

发表于 2023-6-29 16:03:32 | 显示全部楼层
dxf17 2023-6-29 16:03:32 看全部
其实我一直觉得楼主的品味不错!呵呵!知鸟论坛太棒了!
回复

使用道具 举报

发表于 2023-6-29 17:40:07 | 显示全部楼层
我是的十八簿 2023-6-29 17:40:07 看全部
论坛不能没有像楼主这样的人才啊!我会一直支持知鸟论坛
回复

使用道具 举报

发表于 2023-6-29 19:14:15 | 显示全部楼层
墙和鸡蛋 2023-6-29 19:14:15 看全部
既然你诚信诚意的推荐了,那我就勉为其难的看看吧!知鸟论坛不走平凡路。
回复

使用道具 举报

发表于 2023-6-29 19:29:31 | 显示全部楼层
永远爱你冰塘 2023-6-29 19:29:31 看全部
楼主发贴辛苦了,谢谢楼主分享!我觉得知鸟论坛是注册对了!
回复

使用道具 举报

发表于 2023-6-29 23:11:09 | 显示全部楼层
123456848 2023-6-29 23:11:09 看全部
论坛不能没有像楼主这样的人才啊!我会一直支持知鸟论坛
回复

使用道具 举报

发表于 2023-6-29 23:14:26 | 显示全部楼层
风来时狂放 2023-6-29 23:14:26 看全部
楼主,大恩不言谢了!知鸟论坛是最棒的!
回复

使用道具 举报

发表于 2023-6-30 08:34:08 | 显示全部楼层
永远就三年疗 2023-6-30 08:34:08 看全部
其实我一直觉得楼主的品味不错!呵呵!知鸟论坛太棒了!
回复

使用道具 举报

  • 您可能感兴趣
点击右侧快捷回复 【请勿灌水】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则 返回列表

RSS订阅| SiteMap| 小黑屋| 知鸟论坛
联系邮箱E-mail:zniao@foxmail.com
快速回复 返回顶部 返回列表