IIS跨域重定向POST?

时间:2014-11-20 16:12:18

标签: asp.net iis cross-domain http-post url-redirection

是否可以让ASP.NET Web代理接受来自客户端的GET请求,对外部网站执行POST,然后以重定向的形式将外部POST响应发送回原始客户端(URL改变地址栏?)

客户端按如下方式调用我的代理:internalWebsite.com?userId=1234

服务器端,然后创建指向外部URL的HttpWebRequest(POST),获取userId查询字符串以构建令牌并将其放在HttpWebRequest Authorization标头中,外部站点需要进行身份验证。

当此响应返回时,我将外部HTML响应字符串返回给原始客户端。问题是HTML页面有相对链接到图像,css,页面不起作用,因为浏览器仍然认为它在我的内部网站上。

我已经看过许多做外部GET的例子,甚至是外部POST,只要我不需要授权标题,但我无法让它工作。请参阅下面的代码......

string url = "externalWebSite.com/login";

StringBuilder postData = new StringBuilder();
postData.Append("post data goes here");

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

WebHeaderCollection myWebHeaderCollection = request.Headers;

myWebHeaderCollection.Add("Authorization", "Bearer" + " " + token);
request.Headers = myWebHeaderCollection;

request.AllowAutoRedirect = true; //new
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postData.ToString().Length;                        
//request.UseDefaultCredentials = true;
request.UserAgent = ".NET Web Proxy";
request.Referer = url;

StreamWriter writer = null;

try
{
    writer = new StreamWriter(request.GetRequestStream());
    writer.Write(postData.ToString());
    writer.Flush();

    HttpWebResponse WebResp = (HttpWebResponse)request.GetResponse();

    //Now, we read the response (the string), and output it.   
    Stream Answer = WebResp.GetResponseStream();
    StreamReader _Answer = new StreamReader(Answer);

    _context.Response.ContentEncoding = Encoding.UTF8;
    _context.Response.ContentType = WebResp.ContentType;
    _context.Response.Write(_Answer.ReadToEnd());
}
finally
{
    if (writer != null)
        writer.Close();
}

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

收到内容后,尝试插入html-tag" base"就在" head"部分开始:

<html>
 <head>
  <base href="http://externalWebSite.com">
   ... <!-- content from externalWebSite.com -->

此外,如果您要显示收到的所有内容,最好覆盖页面上的渲染方法:

protected override void Render(HtmlTextWriter writer)
{
    string result = GetContentFromTherOtherSite();
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    base.Render(htw);
    writer.Write(result);
}

在这种情况下,在您的网页上,只有来自其他网站的内容,没有您的代码。