C# 仿刷-框架MvcThrottle的使用

1.介绍

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

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

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

 

2.使用

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

https://github.com/stefanprodan/MvcThrottle

2)引入MvcThrottle项目、包

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

image

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)]

 

 

可以关注本人的公众号,多年经验的原创文章共享给大家。

posted on 2016-12-20 18:36 alun-chen 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/alunchen/p/6203789.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值