进程管理运用,防盗链,权限限制

图片防止盗链

 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" />

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值