//第一种方法,Global文件重写
protected void Application_Start(object sender, EventArgs e)
{
File.AppendAllText(Server.MapPath("global.txt"),DateTime.Now.ToString()+" AppStart\r\n");
}
protected void Session_Start(object sender, EventArgs e)
{
File.AppendAllText(Server.MapPath("global.txt"), DateTime.Now.ToString()+" ip:"+Request.UserHostAddress+ " SessionStart \r\n");
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
//每次打开网页都会发生
//File.AppendAllText(Server.MapPath("global.txt"), DateTime.Now.ToString() + " ip:" + Request.UserHostAddress + "request\r\n");
可以用来屏蔽指定ip
//if (HttpContext.Current.Request.UserHostAddress == "127.0.0.1")
//{
// HttpContext.Current.Response.Write("已被屏蔽");
// HttpContext.Current.Response.End();
//}
//防止图片盗链
if (HttpContext.Current.Request.Url.AbsolutePath.EndsWith(".jpg") && HttpContext.Current.Request.UrlReferrer.Host != "localhost")
{
//Context.Response.Write(HttpContext.Current.Request.UrlReferrer.Host);
HttpContext.Current.Response.WriteFile( HttpContext.Current.Server.MapPath("~/dl.jpg"));
HttpContext.Current.Response.End();
}
//符合格式时,内部重写,地址栏不显示此地址
Regex reg = new Regex(@".+MyStatic-(\d+).aspx");
var match = reg.Match(HttpContext.Current.Request.Url.AbsolutePath);
if(match.Success)