简体   繁体   中英

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 . I have set up a 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. 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. 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.

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.

You can find more info about this here:
Microsoft Azure CreateQueue using Simple REST Client
https://github.com/ytechie/event-hubs-sas-generator

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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