首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过url限制IIS中的请求速率

问通过url限制IIS中的请求速率
EN

Stack Overflow用户
提问于 2020-03-18 12:51:00
回答 1查看 3.3K关注 0票数 4

在我的应用程序中,我希望将请求限制到我网站的登录页,由服务器处理,如果请求按特定数量增加,则IIS应阻止该ip地址一段时间。我已经通过IIS的IP地址和域限制‘功能,但我想阻止的请求,如果请求来登录页面,而不是我的网站上的任何其他页面和操作的请求。我们如何使用IIS实现这一点?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-19 10:32:35

IIS IP限制只能用于站点级别。您不能为特定控制器或文件夹设置动态IP限制。建议使用自定义httpmodule。您可以向此代码添加一个筛选器,以便httpmodule只对登录页面的命中号码进行身份验证。

CS1

代码语言:javascript
复制
public class UrlReWrite : IHttpModule
        {

            private int rowCount = Convert.ToInt32(ConfigurationManager.AppSettings["HttpRowCount"]);

            private int httpTime = Convert.ToInt32(ConfigurationManager.AppSettings["HttpTime"]);
            public void Init(HttpApplication application)
            {
                application.BeginRequest += (new
                   EventHandler(this.Application_BeginRequest));
                application.EndRequest += (new
                   EventHandler(this.Application_EndRequest));
            }
            private void Application_BeginRequest(Object source, EventArgs e)
            {
                HttpApplication Application = (HttpApplication)source;
                HttpContext ctx = Application.Context;

                string isIp = ctx.Request.UserHostAddress;
                if (ctx.Application["time"] == null)
                {
                    ctx.Application["time"] = DateTime.Now;
                }
                else
                {
                    DateTime isTime = (DateTime)ctx.Application["time"];
                    int timeTract = Convert.ToInt32(DateTime.Now.Subtract(isTime).Minutes.ToString());
                    if (timeTract > (httpTime - 1))
                    {
                        ctx.Application["time"] = null;
                        ctx.Application["myip"] = null;
                    }
                }
                if (ctx.Application["myip"] != null && ctx.Application["myip"] is CartIp)
                {
                    CartIp cartIp = (CartIp)ctx.Application["myip"];
                    cartIp.Insert(isIp);
                    ctx.Application["myip"] = cartIp;
                    if (cartIp.GetCount(isIp) > rowCount)
                    {
                        ctx.Response.Clear();
                        ctx.Response.Close();
                    }
                }
                else
                {
                    CartIp cartIp = new CartIp();
                    cartIp.Insert(isIp);
                    HttpContext.Current.Application["myip"] = cartIp;
                }
            }
            private void Application_EndRequest(Object source, EventArgs e)
            {
            }
            public void Dispose()
            {
            }
        }
    }

class2.cs

代码语言:javascript
复制
[Serializable]
    public class ListIp
    {
        private string ip;
        private int count;

        public string IP
        {
            get { return ip; }
            set { ip = value; }
        }

        public int Count
        {
            get { return count; }
            set { count = value; }
        }
    }
    [Serializable]
    public class CartIp
    {
        public CartIp()
        {
            if (_listIp == null)
            {
                _listIp = new List<ListIp>();
            }
        }
        private List<ListIp> _listIp;
        public List<ListIp> _ListIp
        {
            get { return _listIp; }
            set { _listIp = value; }
        }

        public void Insert(string ip)
        {
            int indexof = ItemLastInfo(ip);
            if (indexof == -1)
            {

                ListIp item = new ListIp();
                item.IP = ip;
                _listIp.Add(item);
            }
            else
            {
                _listIp[indexof].Count += 1;
            }
        }

    public int ItemLastInfo(string ip)
    {
        int index = 0;
        foreach (ListIp item in _ListIp)
        {
            if (item.IP == ip)
            {
                return index;
            }
            index += 1;
        }
        return -1;
    }
    /// <summary>
    /// get number of IP address
    /// </summary>
    /// <param name="ip"></param>
    /// <returns></returns>
    public int GetCount(string ip)
    {
        foreach (ListIp item in _ListIp)
        {
            if (item.IP == ip)
            {
                return item.Count;
            }
        }
        return -1;
    }
}

web.config

代码语言:javascript
复制
<appSettings>
<add key="HttpRowCount" value="100"/>
<add key="HttpTime" value="10"/>
</appSettings>

您只需要创建一个类库。然后复制并修改这些代码以满足您的需求。最后,您需要将发布的dll复制到bin文件夹,并通过IIS管理器->站点节点->模块->添加托管模块导入它。

https://www.cnblogs.com/Fooo/archive/2013/01/27/2878820.html

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60733707

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档