簡體   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