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

6
  • 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. Commented Jan 24, 2018 at 14:17
  • @ADyson I need to be able to add additional headers and content to the request, see paragraph two. Commented Jan 24, 2018 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? Commented Jan 24, 2018 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. Commented Jan 24, 2018 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. Commented Jan 24, 2018 at 15:48

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this answer - very helpful! Though, I had to Nuget this package: Microsoft.AspNet.WebApi.WebHost

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.