图片防止盗链
void Application_BeginRequest(object sender, EventArgs e)
{
if (Request.RawUrl.Contains("images/"))
{
if (Request.UrlReferrer == null || !IsSameDomain(Request.UrlReferrer, Request.Url))
{
Response.ContentType = "image/jpeg";
string path = Request.MapPath("~/daolian.jpg");
Response.WriteFile(path);
//结束请求
Response.End();
}
}
}
//判断两个域名是否相等
bool IsSameDomain(Uri u1,Uri u2)
{
return Uri.Compare(u1, u2, UriComponents.HostAndPort, UriFormat.SafeUnescaped, StringComparison.CurrentCultureIgnoreCase) == 0 ? true : false;
}
权限判断
public void Init(HttpApplication context)
{
//获得状态 AcquireRequestState
context.AcquireRequestState += new EventHandler(context_AcquireRequestState);
}
void context_AcquireRequestState(object sender, EventArgs e)
{
//验证权限
HttpApplication app = sender as HttpApplication;
if (app != null)
{
if (!app.Request.RawUrl.ToLower().Contains("login.aspx"))
{
if (app.Session["user"] == null)
{
app.Response.Write("<script>alert('没有权限');window.location.href='Login.aspx?returnurl="+app.Request.RawUrl+"'</script>");
app.Response.End();
}
}
}
}
details.aspx?id=1
/details-01.htm
url重写
一、原理
void Application_BeginRequest(object sender, EventArgs e)
{
//url重写
HttpApplication app = sender as HttpApplication;
string url = app.Request.RawUrl;
Regex r = new Regex("/(\\d+)/details\\.htm",RegexOptions.IgnoreCase);
Match m = r.Match(url);
if (m.Success)
{
string id = m.Groups[1].Value;
app.Context.RewritePath("~/PhotoDetails.aspx?id=" + id);
}
}
二、urlRewriter
1、在<configSections>节点加入
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
2、在</configSections>之后加入
<RewriterConfig>
<Rules>
<RewriterRule>
<LookFor>~/(\d{4})/(\d{2})/Default\.aspx</LookFor>
<SendTo>~/Default.aspx?ID=$1</SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>
3、<httpHandlers>中加入
<add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />