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

C#限制网站iis动态IP访问次数源代码

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

namespace YourModuleNameHere

{

public class IPBlackList : IHttpModule

{

        private EventHandler onBeginRequest;


        public IPBlackList()

       {

            onBeginRequest = new EventHandler(this.HandleBeginRequest);

       }


        void IHttpModule.Dispose()

        {

        }


        void IHttpModule.Init(HttpApplication context)

        {

           context.BeginRequest += onBeginRequest;

        }


        const string BLOCKEDIPSKEY = "blockedips";

       const string BLOCKEDIPSFILE = "SiteConfig/blockedips.config";


       public static StringDictionary GetBlockedIPs(HttpContext context)

        {

            StringDictionary ips = (StringDictionary)context.Cache[BLOCKEDIPSKEY ];

            if (ips == null)

            {

                ips = GetBlockedIPs(GetBlockedIPsFilePathFromCurrentContext(context));

                context.Cache.Insert(BLOCKEDIPSKEY , ips, new CacheDependency(GetBlockedIPsFilePathFromCurrentContext(context)));

            }

            return ips;

        }


        private static string BlockedIPFileName = null;

       private static object blockedIPFileNameObject = new object();

       public static string GetBlockedIPsFilePathFromCurrentContext(HttpContext context)

        {

           if (BlockedIPFileName != null)

                return BlockedIPFileName;

           lock(blockedIPFileNameObject)

            {

               if (BlockedIPFileName == null)

                {

                  BlockedIPFileName = context.Server.MapPath(BLOCKEDIPSFILE);

               }

           }

            return BlockedIPFileName;

       }


        public static StringDictionary GetBlockedIPs(string configPath)

        {

            StringDictionary retval = new StringDictionary();

            using (StreamReader sr = new StreamReader(configPath))

            {

               String line;

                while ((line = sr.ReadLine()) != null)

                {

                    line = line.Trim();

                   if (line.Length != 0)

                   {

                       retval.Add(line, null);

                   }

               }

            }

           return retval;

        }


        private void HandleBeginRequest( object sender, EventArgs evargs )

        {

           HttpApplication app = sender as HttpApplication;


           if ( app != null )

            {

                string IPAddr = app.Context.Request.ServerVariables["REMOTE_ADDR"];

                if (IPAddr == null || IPAddr.Length == 0)

                {

                  return;

                }


              StringDictionary badIPs = GetBlockedIPs(app.Context);

               if (badIPs != null && badIPs.ContainsKey(IPAddr))

                {

                   app.Context.Response.StatusCode = 404;

                  app.Context.Response.SuppressContent = true;

                    app.Context.Response.End();

                    return;

              }

           }

       }

    }

}


版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。
·上一条:C#快速导出TXT文件 | ·下一条:windows查看iis网站访问量情况

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

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