专业网站建设品牌,十四年专业建站经验,服务6000+客户--广州京杭网络
免费热线:400-683-0016      微信咨询  |  联系我们

C# 仿刷-框架MvcThrottle的使用

当前位置:网站建设 > 技术支持
资料来源:网络整理       时间:2023/2/14 1:11:19       共计:3634 浏览

1.介绍

1)用MvcThrottle你能保护你的网站不受攻击、刷。

2)你可以限制与设置多个不同场景允许的IP,设置 每秒/分/天 允许访问IP。

3)你可以定义限制,来处理所有请求。或者某个Controller、方法的范围。

 

2.使用

1)首先,请到github上下载框架,里面包括demo。但是demo写得我看不到,读者如果看得懂,建议不用阅读本文。

https://github.com/stefanprodan/MvcThrottle

2)引入MvcThrottle项目、包

如下,我们新建的一个MVC项目WebApplicationIP

3)在FilterConfig类中添加配置

namespace WebApplicationIP
{ public class FilterConfig
    { public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        { const int secondCount = 5; var throttleFilter = new ThrottlingFilter
            { //每秒钟最多请求secondCount次,每分钟最多请求secondCount*60次,依次类推  Policy = new ThrottlePolicy(
                    perSecond: secondCount, 
                    perMinute: secondCount * 10, 
                    perHour: secondCount * 10 * 5, 
                    perDay: secondCount * 10 * 5 * 2)
                {
                    IpThrottling = true },
                Repository = new CacheRepository()
            };
            filters.Add(throttleFilter);

            filters.Add(new HandleErrorAttribute());
        }
    }
}

 

4)在controller的方法设置访问限制

下面是代表用全局的IP访问限制:

[EnableThrottling]

下面是代码这个方法,每秒最多访问5次,每分钟10次:

[EnableThrottling(PerSecond = 5, PerMinute = 10)]

 

版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。
·上一条:WebApi 限制接口访问频率 | ·下一条:solr与.net系列课程(五)solrnet的使用

Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有    粤ICP备16019765号 

广州京杭网络科技有限公司 版权所有