简体   繁体   English

.NET WebApi中的Server.Transfer /重定向

[英]Server.Transfer/Redirection in .NET WebApi

Our company has an internal API (.NET WebApi) serving many of our applications. 我们公司有一个内部API(.NET WebApi)服务于我们的许多应用程序。 Now, we're going to have an application hosted outside and the decision was to not open our main server to the internet. 现在,我们将要在外部托管一个应用程序,而决定是不将主服务器打开到Internet。

Instead, they are going to create a wrapper in a separated server that is properly setup in our firewall and does have access to our network. 相反,他们将在单独的服务器中创建包装器,该包装器已在我们的防火墙中正确设置并且可以访问我们的网络。

The problem is: We want this wrapper API to be as simple as possible and basically redirect requests to our internal API. 问题是:我们希望这个包装器API尽可能简单,并且基本上将请求重定向到我们的内部API。

Something like this: 像这样:

[HttpGet]
[Route("api/PDF/{orderid}")]
public IHttpActionResult GetOrderPDF(string orderid, bool showpictures = false, string disposition = "attachment")
{
    string url = "http://MYINTERNALSERVER/api/PDF/" + orderid;
    System.Uri uri = new System.Uri(url);
    return Redirect(uri); //Server.Transfer ?
}

The problem about using this method is that my external client will try to access our internal server and, of course, will not have access. 使用此方法的问题是我的外部客户端将尝试访问我们的内部服务器,并且当然将没有访问权限。

We want to have a wrapper so we only expose methods that we want. 我们想要一个包装器,所以我们只公开我们想要的方法。

I read about IIS Redirection but if I redirect all requests, I'm pretty much exposing all methods to the internet. 我读过有关IIS重定向的信息,但是如果我重定向所有请求,则几乎会将所有方法公开给Internet。

Any suggestions ? 有什么建议么 ?

The first time you need to do a POST request, this approach is going to fall apart because you cannot redirect a POST operation. 第一次需要执行POST请求时,此方法将崩溃,因为您无法重定向POST操作。

You don't need a whole new application. 您不需要全新的应用程序。 What you need is a reverse-proxy (consider NGINX ) that is publicly available to the internet. 您需要的是反向代理(考虑NGINX ),该代理可以在Internet上公开使用。 It can proxy traffic across the firewall to whatever is serving up WebAPI. 它可以将通过防火墙的流量代理到提供WebAPI服务的任何内容。

The benefits here are: 这里的好处是:

  • If there is an issue with outside traffic (hacking), you can disable the reverse-proxy without disturbing internal traffic 如果外部流量存在问题(黑客),则可以禁用反向代理,而不会干扰内部流量
  • If you take the current approach, or try to write your own proxy, you'll need to modify it any time your WebAPI contract changes. 如果采用当前方法,或尝试编写自己的代理,则只要WebAPI合同发生变化,就需要对其进行修改。 You won't need to do this with a reverse proxy, because the proxy is designed to be mostly transparent to the HTTP traffic it is serving. 您无需使用反向代理来执行此操作,因为代理被设计为对其所服务的HTTP流量几乎是透明的。
  • No code to write. 没有代码可写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM