简体   繁体   English

Azure Web应用程序服务使用混合连接管理器使用HttpClient调用本地WEB API

[英]Azure web app service to call onpremise WEB API using HttpClient using hybrid connection manager

We have onpremise web service (asp.net core mvc) deployed on local network machine. 我们在本地网络计算机上部署了本地Web服务(asp.net核心mvc)。 We are trying to call these WEB API using App Service deployed on Azure. 我们正在尝试使用部署在Azure上的App Service调用这些WEB API。 But it is giving time out error or Task was cancelled error in case when we try to connect it using "HTTP" Protocol. 但是如果我们尝试使用“ HTTP”协议连接它,则会发出超时错误或任务被取消错误。 In case of "HTTPS" it is giving " security error occurred error ". 如果是“ HTTPS”,则给出“ 安全错误发生错误 ”。

We have created Hybrid connection on Azure App Service to connect to onpremise web api service which shows online for both 80 and 443 port. 我们已经在Azure App Service上创建了Hybrid连接,以连接到本地Web api服务,该服务在线显示80和443端口。 We have setup Hybrid Connection Manager on local network machine too. 我们也在本地网络计算机上设置了混合连接管理器。

Below is the code snippet for calling code which is deployed on Azure App Service (eg https://xyz.azurewebsite.com ) 以下是调用代码的代码段,该代码段部署在Azure App Service(例如https://xyz.azurewebsite.com )上

 try
 {
    var httpClient = new HttpClient();
    httpClient.Timeout = TimeSpan.FromMinutes(60);
    httpClient.BaseAddress = new Uri("https://onpremise");
    httpClient.DefaultRequestHeaders.Accept.Clear();
    httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
    ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
    //Simple Get Request to test on-premise service
    var response = await httpClient.GetStringAsync("api/OnPremiseData/GetData");
    ViewBag.ResponseText = response;
 }

Above code works fine in case I debug application from localhost. 如果我从本地主机调试应用程序,则上述代码可以正常工作。 So there is no issue with code I assume. 因此,我认为代码没有问题。

Below is web api code snippet: 以下是Web API代码段:

[Route("api/[controller]/[action]")]
public class OnPremiseDataController : Controller
{        
  [HttpGet]
  public string GetData()
  {
    return "Success";
  }
}

and below is startup.cs file 下面是startup.cs文件

public void ConfigureServices(IServiceCollection services)
{
  services.AddCors();
  services.AddMvc();            
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
  app.UseCors(options => options.WithOrigins("https://xyz.azurewebsite.com", "https://localhost:44310").AllowAnyMethod().AllowAnyHeader());
  if (env.IsDevelopment())
  {
    app.UseDeveloperExceptionPage();
  }            
  app.UseMvc();                
}

我们有类似的情况(本地WebApp和SQL),诀窍是在本地计算机上的Hybrid Connection Manager中使用端点的完全限定域名。

Below is the solution for above question. 以下是上述问题的解决方案。 Basically I have solved 2 problem. 基本上我已经解决了2个问题。

First is using FQDN (fully qualified domain name) I am able to connect to on-premise services. 首先是使用FQDN(完全限定域名),我可以连接到本地服务。 Please make sure you are using FQDN while configuring endpoint in Hybrid connection on Azure. 请确保在Azure上的混合连接中配置终结点时正在使用FQDN

Second, using below line of code I am able to make secure HTTPS request ot on-premise server: 其次,使用下面的代码行,我可以在本地服务器上发出安全的HTTPS请求:

    HttpMessageHandler handler = new HttpClientHandler
    {
         SslProtocols = System.Security.Authentication.SslProtocols.Tls12 | System.Security.Authentication.SslProtocols.Tls11 | System.Security.Authentication.SslProtocols.Tls,
         ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true
    };

Below is complete solution of above problem : 以下是上述问题的完整解决方案:

    HttpMessageHandler handler = new HttpClientHandler
                    {
                        SslProtocols = System.Security.Authentication.SslProtocols.Tls12 | System.Security.Authentication.SslProtocols.Tls11 | System.Security.Authentication.SslProtocols.Tls,
                        ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true
                    };
var httpClient = new HttpClient(handler);
    httpClient.Timeout = TimeSpan.FromMinutes(60);
        httpClient.BaseAddress = new Uri("https://onpremise");
        httpClient.DefaultRequestHeaders.Accept.Clear();
        httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        //Simple Get Request to test on-premise service
        var response = await httpClient.GetStringAsync("api/OnPremiseData/GetData");
        ViewBag.ResponseText = response;

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

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