本文介绍了Azure Web应用程序服务使用混合连接管理器使用HttpClient调用本地WEB API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在本地网络计算机上部署了本地Web服务(asp.net核心mvc).我们正在尝试使用部署在Azure上的App Service调用这些WEB API.但是,如果我们尝试使用"HTTP"协议进行连接,则会发出超时错误或任务已取消错误.如果是"HTTPS",则会显示"发生安全错误错误".

We have onpremise web service (asp.net core mvc) deployed on local network machine. We are trying to call these WEB API using App Service deployed on Azure. But it is giving time out error or Task was cancelled error in case when we try to connect it using "HTTP" Protocol. In case of "HTTPS" it is giving "security error occurred error".

我们在Azure App Service上创建了Hybrid连接,以连接到本地Web api服务,该服务在线显示80和443端口.我们也在本地网络计算机上也设置了混合连接管理器.

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. We have setup Hybrid Connection Manager on local network machine too.

以下是用于调用在Azure应用服务上部署的代码的代码段(例如 https://xyz.azurewebsite. com )

Below is the code snippet for calling code which is deployed on Azure App Service (e.g. 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;
 }

如果我从localhost调试应用程序,则上述代码可以正常工作.因此,我认为代码没有问题.

Above code works fine in case I debug application from localhost. So there is no issue with code I assume.

下面是Web api代码段:

Below is web api code snippet:

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

及以下是startup.cs文件

and below is startup.cs file

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();                
}

推荐答案

以下是上述问题的解决方案.基本上我已经解决了2个问题.

Below is the solution for above question. Basically I have solved 2 problem.

首先使用FQDN(完全限定域名),我能够连接到本地服务.在Azure上的混合连接中配置终结点时,请确保您正在使用FQDN .

First is using FQDN (fully qualified domain name) I am able to connect to on-premise services. Please make sure you are using FQDN while configuring endpoint in Hybrid connection on Azure.

第二,使用下面的代码行,我可以在本地服务器上进行安全的HTTPS请求:

Second, using below line of code I am able to make secure HTTPS request ot on-premise server:

    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;

这篇关于Azure Web应用程序服务使用混合连接管理器使用HttpClient调用本地WEB API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 15:28