zoukankan      html  css  js  c++  java
  • 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;
           }

        

        }

     
    }

  • 相关阅读:
    Google Chart API 阮一峰的网络日志
    PHP随机函数【上】
    php实现socket推送技术
    javascript变量作用域
    如何使用jqplot描绘一个简单的线形图?
    培训小记
    Google自己的浏览器GoogleChrome
    这大半年的回顾
    一个高手的SQL求工作天数的函数
    关于TSQL中数据库重命名
  • 原文地址:https://www.cnblogs.com/tiancai/p/2315016.html
Copyright © 2011-2022 走看看