I'm trying to implement a simple proxy within a .NET Core REST service, so I can inject additional authentication headers, and then return it to any client like a normal website.
In a simplified form it looks like this:
[HttpGet]
public async Task<ContentResult> Get()
{
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://google.com");
/* some extra headers injection happens here */
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
var result = await response.Content.ReadAsStringAsync();
return Content(result, "text/html", Encoding.UTF8);
}
The problem is that while the response is correctly rendered by any browser as the original HTML page, any script
or link
(any relative URL) inclusion in the returned page fails.
What is missing in the code above to make browsers resolve inner relative URL-s correctly?
In the above example, if I run it, I get google.com
page displayed from my https://localhost:44307/api/test
, except images and other stuff from relative URL-s is missing, as they fail to resolve inner relative URL-s.
In a confusion, I tried to play with such properties as Referer
and Host
within request and response, but didn't make any progress.
Where it is needed. We need to use a third-party website via IFRAME, and that website requires Authorization
header present, so the proxy above is supposed to do just that, and then return the website, so the API link can be used directly, like this: <iframe src="https://localhost:44307/api/test">
- this example should render complete google.com
website inside the iframe, but it renders HTML only.