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