void app_BeginRequest(object sender, EventArgs e) { HttpApplication app = (HttpApplication)sender; if (app.Request.Path == "/" && app.Request.QueryString["target"] == "clear") { RemoveProxySiteCookie(app.Response); app.Response.Redirect("/"); return; } string destRoot = GetProxySiteFromCookie(app); if (string.IsNullOrEmpty(destRoot)) { return; } string destUrl = destRoot + app.Request.RawUrl; string srcUrl = app.Request.Url.AbsoluteUri; HttpProxyHandler hander = new HttpProxyHandler(srcUrl, destUrl); app.Context.RemapHandler(hander); app.Context.Response.Headers.Add("X-CookieProxyModule", destUrl); // 用于调试诊断 }
private void App_BeginRequest(object sender, EventArgs e) { HttpApplication app = (HttpApplication)sender; // 尝试从配置规则中获取目标站点地址 string destRoot = GetProxySite(app); // 如果没有匹配项,则表示从当前站点中处理 if (string.IsNullOrEmpty(destRoot)) { return; } // 计算用转发的实际网址 string destUrl = destRoot + app.Request.RawUrl; string srcUrl = app.Request.Url.AbsoluteUri; HttpProxyHandler hander = new HttpProxyHandler(srcUrl, destUrl); app.Context.RemapHandler(hander); app.Context.Response.Headers.Add("X-HttpProxyModule", destUrl); // 用于调试诊断 // 既然转发了请求,就不用当前站点的身份认证了 app.Context.SkipAuthorization = true; }
/// <summary> /// 以异步方式执行HttpHanlder(基类方法重写) /// </summary> /// <param name="context"></param> /// <returns></returns> public async override Task ProcessRequestAsync(HttpContext context) { string destUrl = GetTransferTarget(context); if (string.IsNullOrEmpty(destUrl)) { throw new ArgumentNullException("destUrl"); } string srcUrl = context.Request.Url.AbsoluteUri; context.Response.Headers.Add("X-ProxyTransferHandler", destUrl); // 用于调试诊断 HttpProxyHandler hander = new HttpProxyHandler(srcUrl, destUrl); await hander.ProcessRequestAsync(context); }