加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 根据IP地址限制对Elmah的访问

发布时间:2020-12-16 06:23:13 所属栏目:asp.Net 来源:网络整理
导读:我需要限制对elmah.axd的访问. elvue.html基于某些IP地址.该网站位于IIS 6.0和.Net 3.5上.我不能使用Forms身份验证或 Windows身份验证.我正在考虑使用http模块的方法. http://www.codeproject.com/Articles/16384/Using-ASP-NET-HTTP-Modules-to-restrict-ac
我需要限制对elmah.axd的访问. elvue.html基于某些IP地址.该网站位于IIS 6.0和.Net 3.5上.我不能使用Forms身份验证或 Windows身份验证.我正在考虑使用http模块的方法.
http://www.codeproject.com/Articles/16384/Using-ASP-NET-HTTP-Modules-to-restrict-access-by-I

我不能使用限制web.config访问的方法,因为那只适用于IIS 7.
http://boseca.blogspot.com/2010/12/programmatically-addremove-ip-security.html

是否有人指出我如何解决这个问题?请指教.

解决方法

我使用以下代码实现了这个:

protected void Application_BeginRequest(Object sender,EventArgs e)
        {
            var ClientSourceIP = Context.Request.Headers["CLIENT_SRC_IP"];
            var HTTPClientSourceIP = Context.Request.Headers["HTTP_CLIENT_SRC_IP"];
            var isValidClientSourceIP = ClientSourceIP == null || 
                Regex.IsMatch(ClientSourceIP,ConfigurationManager.AppSettings["ValidClientSourceIP"]);
            var isValidHTTPClientSourceIP = HTTPClientSourceIP == null || 
                Regex.IsMatch(HTTPClientSourceIP,ConfigurationManager.AppSettings["ValidHTTPClientSourceIP"]);

            if (
                (Context.Request.Path.Contains("elmah.axd") || Context.Request.Path.Contains("elvue.aspx")) &&
                ((isValidClientSourceIP && isValidHTTPClientSourceIP) == false)
            )
            {
                this.Context.Response.StatusCode = 403;
                return;
            }
        }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读