<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>allow proxy asp net core &#8211; ASP.NET Hosting Reviews and Guides</title>
	<atom:link href="https://topreviewhostingasp.net/tag/allow-proxy-asp-net-core/feed/" rel="self" type="application/rss+xml" />
	<link>https://topreviewhostingasp.net</link>
	<description>ASP.NET Hosting &#124; Reviews &#124; Tips &#38; Tutorial</description>
	<lastBuildDate>Mon, 11 Jul 2022 05:10:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://topreviewhostingasp.net/wp-content/uploads/2017/01/cropped-trhaico-32x32.png</url>
	<title>allow proxy asp net core &#8211; ASP.NET Hosting Reviews and Guides</title>
	<link>https://topreviewhostingasp.net</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to Allow Proxy to Another URL with ASP.NET Core</title>
		<link>https://topreviewhostingasp.net/how-to-allow-proxy-to-another-url-with-asp-net-core/</link>
					<comments>https://topreviewhostingasp.net/how-to-allow-proxy-to-another-url-with-asp-net-core/#respond</comments>
		
		<dc:creator><![CDATA[Jacques Hunt]]></dc:creator>
		<pubDate>Mon, 11 Jul 2022 05:06:57 +0000</pubDate>
				<category><![CDATA[Hosting Tips]]></category>
		<category><![CDATA[allow proxy asp net core]]></category>
		<category><![CDATA[asp net core]]></category>
		<category><![CDATA[asp net core proxy]]></category>
		<category><![CDATA[asp net core tips]]></category>
		<category><![CDATA[asp net core tutorial]]></category>
		<guid isPermaLink="false">https://topreviewhostingasp.net/?p=3067</guid>

					<description><![CDATA[This post talks about writing a simple HTTP proxy logic in C# or ASP.NET Core. And allowing your project to proxy the request to any other URL. It is not about deploying a proxy server for your ASP.NET Core project. Before starting, you need to be clear about the target you are going to proxy [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>This post talks about writing a simple HTTP proxy logic in C# or ASP.NET Core. And allowing your project to proxy the request to any other URL. It is not about deploying a proxy server for your ASP.NET Core project.</p>



<p>Before starting, you need to be clear about the target you are going to proxy for. It shall be an URL.</p>



<p>Add the following code anywhere of your project.</p>



<pre class="wp-block-code"><code>        public static HttpRequestMessage CreateProxyHttpRequest(this HttpContext context, Uri uri)
        {
            var request = context.Request;

            var requestMessage = new HttpRequestMessage();
            var requestMethod = request.Method;
            if (!HttpMethods.IsGet(requestMethod) &amp;&amp;
                !HttpMethods.IsHead(requestMethod) &amp;&amp;
                !HttpMethods.IsDelete(requestMethod) &amp;&amp;
                !HttpMethods.IsTrace(requestMethod))
            {
                var streamContent = new StreamContent(request.Body);
                requestMessage.Content = streamContent;
            }

            // Copy the request headers
            foreach (var header in request.Headers)
            {
                if (!requestMessage.Headers.TryAddWithoutValidation(header.Key, header.Value.ToArray()) &amp;&amp; requestMessage.Content != null)
                {
                    requestMessage.Content?.Headers.TryAddWithoutValidation(header.Key, header.Value.ToArray());
                }
            }

            requestMessage.Headers.Host = uri.Authority;
            requestMessage.RequestUri = uri;
            requestMessage.Method = new HttpMethod(request.Method);

            return requestMessage;
        }</code></pre>



<p>This method covert user sends <code>HttpContext.Request</code> to a reusable <code>HttpRequestMessage</code>. So you can send this message to the target server.</p>



<p>After your target server response, you need to copy the responded <code>HttpResponseMessage</code> to the <code>HttpContext.Response</code> so the user&#8217;s browser just gets it.</p>



<pre class="wp-block-code"><code>        public static async Task CopyProxyHttpResponse(this HttpContext context, HttpResponseMessage responseMessage)
        {
            if (responseMessage == null)
            {
                throw new ArgumentNullException(nameof(responseMessage));
            }

            var response = context.Response;

            response.StatusCode = (int)responseMessage.StatusCode;
            foreach (var header in responseMessage.Headers)
            {
                response.Headers[header.Key] = header.Value.ToArray();
            }

            foreach (var header in responseMessage.Content.Headers)
            {
                response.Headers[header.Key] = header.Value.ToArray();
            }

            // SendAsync removes chunking from the response. This removes the header so it doesn't expect a chunked response.
            response.Headers.Remove("transfer-encoding");

            using (var responseStream = await responseMessage.Content.ReadAsStreamAsync())
            {
                await responseStream.CopyToAsync(response.Body, _streamCopyBufferSize, context.RequestAborted);
            }
        }</code></pre>



<p>And now the preparation is complete. Back to our controller:</p>



<pre class="wp-block-code"><code>        public async Task&lt;IActionResult&gt; Rewrite()
        {
            var request = HttpContext.CreateProxyHttpRequest(new Uri("https://www.google.com"));
            var response = await _client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, HttpContext.RequestAborted);
            await HttpContext.CopyProxyHttpResponse(response);
            return Ok();
        }</code></pre>



<p>And try to access it. It will be proxied to google.com</p>



<figure class="wp-block-image size-large"><img fetchpriority="high" decoding="async" width="1024" height="560" class="wp-image-3068" src="https://topreviewhostingasp.net/wp-content/uploads/2022/07/proxy-1024x560.png" alt="" srcset="https://topreviewhostingasp.net/wp-content/uploads/2022/07/proxy-1024x560.png 1024w, https://topreviewhostingasp.net/wp-content/uploads/2022/07/proxy-300x164.png 300w, https://topreviewhostingasp.net/wp-content/uploads/2022/07/proxy-768x420.png 768w, https://topreviewhostingasp.net/wp-content/uploads/2022/07/proxy-1536x839.png 1536w, https://topreviewhostingasp.net/wp-content/uploads/2022/07/proxy-2048x1119.png 2048w, https://topreviewhostingasp.net/wp-content/uploads/2022/07/proxy-50x27.png 50w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
]]></content:encoded>
					
					<wfw:commentRss>https://topreviewhostingasp.net/how-to-allow-proxy-to-another-url-with-asp-net-core/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
