繁体   English   中英

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

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

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

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

以下是调用代码的代码段,该代码段部署在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;
 }

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

以下是Web API代码段:

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

下面是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中使用端点的完全限定域名。

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

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

其次,使用下面的代码行,我可以在本地服务器上发出安全的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
    };

以下是上述问题的完整解决方案:

    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