2

I would like to proxy every requests to a web application, pass it to another web application and then return my other web applications response to the original sender.

It should be able to handle all http methods and content-types etc. I should also be able to edit the incoming request and add additional headers and content.

The background for doing this is the security architecture for a project that has one web server in a public DMZ and then another web server in the internal network that is allowed to talk to the database server.

enter image description here

Found a thread for ASP.NET core but preferably it should be done with .Net Framework and not be dependent on an external library.

Creating a proxy to another web api with Asp.net core

Ogglas
  • 38,157
  • 20
  • 203
  • 266
  • 1
    what's your question? If you just want to pass on requests and responses verbatim you don't need another application, just a general gateway server, I'd have thought. Those kinds of tools can probably add the odd extra header etc if needed. – ADyson Jan 24 '18 at 14:17
  • @ADyson I need to be able to add additional headers and content to the request, see paragraph two. – Ogglas Jan 24 '18 at 14:18
  • yes some gateway software I believe can do that. But again, what's your _question_? Also why do you need to pass on requests? Is this "client portal" an API or a GUI? It _sounds_ like a GUI. So user makes request to the GUI, GUI makes request to the API...sounds pretty standard? – ADyson Jan 24 '18 at 14:22
  • @ADyson I'm sorry if the question is poorly asked. I found an answer myself but the reason for the pass of requests is because I need to add a certificate to each request. This can not be handled in the GUI because then the client would have access to this certificate. – Ogglas Jan 24 '18 at 15:21
  • "This can not be handled in the GUI because then the client would have access to this certificate." Not if the GUI's backend (i.e. server-side code) is making the request. Anyway, glad you found a solution. – ADyson Jan 24 '18 at 15:48
  • @ADyson Poorly formulated then because this is exactly what I was after and what my solution is. For me GUI for web application is the client (browser) and API is back end. :) – Ogglas Jan 24 '18 at 15:53

1 Answers1

1

Found a good answer for Web API that led me in the right direction.

https://stackoverflow.com/a/41680404/3850405

I started out by adding a new ASP.NET Web Application -> MVC -> No Authentication.

I then removed everything accept Global.asax, packages.config and Web.config.

I then edited Global.asax to use a DelegatingHandler like this:

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        GlobalConfiguration.Configure(CustomHttpProxy.Register);
    }
}

public static class CustomHttpProxy
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "Proxy",
            routeTemplate: "{*path}",
            handler: HttpClientFactory.CreatePipeline(
                innerHandler: new HttpClientHandler(),
                handlers: new DelegatingHandler[]
                {
                    new ProxyHandler()
                }
            ),
            defaults: new { path = RouteParameter.Optional },
            constraints: null
        );
    }
}

public class ProxyHandler : DelegatingHandler
{
    private static HttpClient client = new HttpClient();

    protected override async Task<HttpResponseMessage> SendAsync(
        HttpRequestMessage request,
        CancellationToken cancellationToken)
    {
        var forwardUri = new UriBuilder(request.RequestUri.AbsoluteUri);
        forwardUri.Host = "localhost";
        forwardUri.Port = 62904;
        request.RequestUri = forwardUri.Uri;

        if (request.Method == HttpMethod.Get)
        {
            request.Content = null;
        }

        request.Headers.Add("X-Forwarded-Host", request.Headers.Host);
        request.Headers.Host = "localhost:62904";
        var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
        return response;
    }
}

After this I had to add the static content and then everything worked.

enter image description here

Ogglas
  • 38,157
  • 20
  • 203
  • 266
  • Thanks for this answer - very helpful! Though, I had to Nuget this package: **Microsoft.AspNet.WebApi.WebHost** – fakir314 Nov 26 '20 at 13:15