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;
       }

    

    }

 

 

 
}

posted @   甜菜波波  阅读(233)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示