简体   繁体   English

使用Azure混合连接连接到内部Web API

[英]Using Azure Hybrid Connection to connect to internal Web API

Very new to Azure, and I have an internal web API on an internal address http://internal-server:182/api/policies . Azure的新手,我在内部地址http:// internal-server:182 / api / policies上有一个内部Web API。 I have set up a Hybrid Connection internal-service.servicebus.windows.net. 我已经设置了Hybrid Connection internal-service.servicebus.windows.net。 This is connected and working. 这已连接并且正在工作。

My struggle is getting the C# code working to connect and retrieve the data. 我的奋斗目标是使C#代码能够连接和检索数据。 After a number of days, I have reviewed various articles, videos etc and all seem more advanced than what I am trying to do, which is just call the Web API and read the JSON. 几天后,我查看了各种文章,视频等,它们似乎都比我尝试做的要先进,后者只是调用Web API并读取JSON。 I have tried to simplify the code but receive the error: 我试图简化代码,但收到错误:

401 MalformedToken: Invalid authorization header: The request is missing WRAP authorization credentials. 

At present I have the followed code: 目前,我有以下代码:

using (var client = new HttpClient())
{
    var url = "http://internal-service.servicebus.windows.net";

    var tp = TokenProvider.CreateSharedAccessSignatureTokenProvider("RootManageSharedAccessKey", "<key goes here>");

    var token = tp.GetWebTokenAsync(url, string.Empty, true, TimeSpan.FromHours(1))
                .GetAwaiter()
                .GetResult();

    client.BaseAddress = new Uri(url);
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));


    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("ServiceBusAuthorization", token);

    var response = client.GetAsync("/api/policies").Result;
    string res = "";
    using (HttpContent content = response.Content)
    {
       // ... Read the string.
       Task<string> result = content.ReadAsStringAsync();
       res = result.Result;

       Label1.Text = res;
     }

} }

Any help or direction would be much appreciated? 任何帮助或指示将不胜感激? Once this code is working the Web App will be published as an Azure Web App. 此代码运行后,该Web App将作为Azure Web App发布。

Seems that your are not sending the right header. 似乎您没有发送正确的标题。

First suggestion: intercept the call with a proxy like fiddler , to do that add a proxy config to your call to localhost port 8888, after this you can try some request and see the raw http you are sending to the server, you can also modify it until it works, once you have this modify your code until it send the same raw http. 第一个建议:使用诸如fiddler之类的代理拦截该呼叫,以在向本地主机端口8888的呼叫中添加代理配置,此后您可以尝试一些请求并查看要发送到服务器的原始http,也可以修改直到它起作用为止,一旦您修改了代码,直到它发送了相同的原始http。

You can find more info about this here: 您可以在此处找到有关此的更多信息:
Microsoft Azure CreateQueue using Simple REST Client 使用简单REST客户端的Microsoft Azure CreateQueue
https://github.com/ytechie/event-hubs-sas-generator https://github.com/ytechie/event-hubs-sas-generator

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

相关问题 Azure Web应用程序服务使用混合连接管理器使用HttpClient调用本地WEB API - Azure web app service to call onpremise WEB API using HttpClient using hybrid connection manager 通过混合连接从客户端连接到Azure Web作业 - Connect To Azure Web Job from Client over Hybrid Connection Azure App服务无需内部连接即可在内部连接到SQL Server - Azure App service connect to SQL Server on premise without hybrid connection Azure 混合连接字符串 - Azure Hybrid Connection string 如何使用 Pulumi 创建 Azure 混合连接? - How do you create an Azure Hybrid Connection using Pulumi? C#Web应用程序未通过Azure混合连接连接到本地SQL Server - C# web app not connecting to on premise SQL Server through Azure hybrid Connection Azure资源管理API。 创建服务总线混合连接时的授权问题 - Azure Resources Management API. Authorization issue when creating Service Bus Hybrid Connection Azure混合连接中的SYSTEM_USER - SYSTEM_USER in Azure Hybrid Connection Microsoft Azure服务总线/混合连接 - Microsoft Azure Service Bus / Hybrid Connection 如何使用C#设置Azure中继混合连接以连接到本地SQL Server? - How to setup azure relay hybrid connections using C# to connect to on-premises SQL Server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM