httpmodule

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Xml;
namespace ClassLibrary1
{

   public class complete:IHttpModule
   {
    

       public void Dispose() { }
       public void Init(HttpApplication context)
       {
           context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
       }
       void context_PreRequestHandlerExecute(object sender, EventArgs e)
       {
        
           HttpApplication ha = (HttpApplication)sender;
           XmlDocument doc = new XmlDocument();
           doc.Load(ha.Context.Server.MapPath("~/ip.xml"));
           XmlNode root = doc.SelectSingleNode("admin");

           foreach (XmlNode xn in root.ChildNodes)
           {
               string ip=xn.InnerText;
               //if (ip=="127.0.0.1")
              
               //{
               //    ha.CompleteRequest();
               //    ha.Response.Write("你被限制登录");
              
               //}
          
           }

           ha.Response.Write(ha.Request.UserHostAddress);
           ha.Response.Write(GetClientIP());

           string path = ha.Context.Request.Url.ToString();
        //   ha.Context.Response.Write(path.IndexOf("admin"));

           int num = path.LastIndexOf("/")-1;
         //  path = path.Replace(".html",".aspx");
         
        //   ha.Server.Transfer("2.aspx");
           if (path.IndexOf("admin") > 0)
           {

               if (ha.Context.Session["name"] == null) //是否Session中有用户名,若是空的话,转向登录页。
               {
               //   ha.Context.Response.Write("<script>alert('请登录');</script>");
                   ha.Context.Response.Write("<script>top.location.href='login.aspx';</script>");
               }
               else
               {

                   ha.Context.Response.Write("<script>alert('登录成功!');</script>");
               }
           }

           else
           {
          
          
           }
       }
   
       // ip

       private static string GetClientIP()
       {
           string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

           if (null == result || result == String.Empty)
           {
               result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
           }

           if (null == result || result == String.Empty)
           {
               result = HttpContext.Current.Request.UserHostAddress;
           }

           return result;
       }

    

    }

 

 

 
}

转载于:https://www.cnblogs.com/tiancai/archive/2012/01/06/2315016.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值