3

Background: We have a requirement to be able to support web servers other than IIS. To do this we are planning on moving all our processing logic to another service which will use a queuing mechanism as a transport (RabbitMQ to be specific). We will have a HTTP web api that will basically forward all requests to this other service; for IIS we will implement this facade in C# & probably Java for other web servers like Tomcat.

My question is this; for c# what is the best way to forward all the HTTP requests & content requests to another service. I've being looking at using a asmx HTTPHandler for this; I have it working; but the solution is ugly and requires using a URL re-writer for clean URLs.

Would anyone know if it is possible to do this using MVC or the web-api itself; we basically take all the content received; the URL itself, cookies, query string etc; package it up and send it on for processing.

Bigtoe
  • 3,372
  • 1
  • 31
  • 47

2 Answers2

0

figured out how to do this using the Web-API and wildcard routes.

Thanks for you input Alexei.

Bigtoe
  • 3,372
  • 1
  • 31
  • 47
  • Are you willing to share your solution? – Wiebe Tijsma Jun 10 '14 at 12:57
  • Hi Zihad, What we've now focused on is using a reverse proxy to forward on requests to an internal HTTP server. Have a read of these links they may be a help http://stackoverflow.com/questions/9999538/how-can-i-setup-reverse-proxy-on-iis-allowing-cross-host-communciation-between. http://tomcat.apache.org/connectors-doc/generic_howto/proxy.html. http://weblogs.asp.net/owscott/creating-a-reverse-proxy-with-url-rewrite-for-iis – Bigtoe Jun 11 '14 at 11:24
  • Ah got it thanks... I just used a single Web API http://myproject/application/{*url} route in Web API to be able to do some transformation and filtering too... Thanks! – Wiebe Tijsma Jun 11 '14 at 14:55
0

(reply to an old question but might be useful to others)

I had a similar problem where we needed to put a facade in front of a web api that only supports basic or kerberos authentication.

I used this solution that transforms the incoming url to the url of the backend server: http://www.dotnetspeak.com/asp-net-mvc/using-webapi-in-multi-tier-web-application/

I added some logic to that solution to check the token (from the asp.net login system) in the header because ExecuteAsync executes before the Authorization filters:

var token = controllerContext.Request.Headers.Authorization;
if (token != null && token.Scheme.Equals("bearer", StringComparison.InvariantCultureIgnoreCase))
{
     var ticket = Startup.OAuthOptions.AccessTokenFormat.Unprotect(token.Parameter);

     if (ticket != null && ticket.Identity != null && ticket.Identity.IsAuthenticated)
     {
          var claimsPrincipal = new ClaimsPrincipal(ticket.Identity);
          //From here, you can use the claimsPrinciple to check if user is allowed to even call the service.
          var authorized = claimsPrincipal.IsInRole("Users");

     }
 }

if Startup.OAuthOptions is not available, you might need to convert this to a static variable in Startup.cs or Startup.Auth.cs.

Because I needed to provide an alternative authentication method instead of the basic authentication of the backend service, an additional updating of the header is added to switch to basic Auth.

//from the dotnetspeak solution (copy existing headers)
foreach (var httpRequestHeader in controllerContext.Request.Headers)
{
    client.DefaultRequestHeaders.Add(httpRequestHeader.Key, httpRequestHeader.Value);
}

//Set basic authentication, whatever the original Authorization header might have been
//TODO: use lookup table or something like that to convert claimsPrinciple to matching domain user account 
var byteArray = Encoding.ASCII.GetBytes(@"Domain\userId:password");
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
Darkness
  • 11
  • 1