Happy Business Starts Here

Student

Basic authentication fails

If I go into Chrome, and test the URL

https://apisandbox-api.zuora.com/rest/v1/transactions/payments/accounts/A00000001

it pops up a box and asks for my username and password, which I enter and it works.

 

Then in ASP .NET  I do

 

           using (HttpClient client = new HttpClient())
            {
                // Like "https://apisandbox-api.zuora.com/rest/v1"
                client.BaseAddress = new Uri(APIURLBase.Text);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
                    "Basic",
                    Convert.ToBase64String(
                        System.Text.ASCIIEncoding.ASCII.GetBytes(
                            string.Format("{0}:{1}", UserNameTextBox.Text, PasswordTextBox.Text))));
                // Like "transactions/payments/accounts/A00000001?pageSize=40"
                HttpResponseMessage response = await client.GetAsync(APIURLRequest.Text);
                if (response.IsSuccessStatusCode)

 

It fails with a Forcable Disconnect error message... An existing connection was forcibly closed by the remote host

Any ideas why this would happen?

Senior Software Engineer
Healthicity / AAPC
6 REPLIES 6
Zuora Support Moderator

Re: Basic authentication fails


Is your integration leveraging a TLS 1.1 or higher connection? By the look of the exception fragment you provided, I've seen similar error when the connection attempt is TLS 1.0 and being rejected as we have recently disabled TLS 1.0 in API Sandbox exclusively. See our Zuora community post on this topic for additional details.



If you found my answer helpful, please give me a kudo ↑
Help others find answers faster by accepting my post as a solution √

Student

Re: Basic authentication fails

Ok, found an alternative....

In my C# code I put

 

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

 

before creating the HttpClient

 

Senior Software Engineer
Healthicity / AAPC
Student

Re: Basic authentication fails

In addition now I get a Not Found error.

At least it is not disconnecting.

 

Any ideas?

Senior Software Engineer
Healthicity / AAPC
Student

Re: Basic authentication fails

Got it...

 

The code had the base URL as 

 

https://apisandbox-api.zuora.com/rest/v1

 

but .NET chops the /v1 off the end.

 

I added the v1/ to the requestURI  so it reads

 

v1/transactions/payments/accounts/A00000001?pageSize=40

 

And now it is successful.   :-)

Senior Software Engineer
Healthicity / AAPC
Student

Re: Basic authentication fails

And in case any one else is dying on this hill.... (ReturnJsonAsHTML not shown because the embedded HTML code in the display method messes up the forum display.)

 

public async Task TestZuoraJSON()
        {
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

            using (HttpClient client = new HttpClient())
            {
                // Like "https://apisandbox-api.zuora.com/rest"
                client.BaseAddress = new Uri(APIURLBase.Text);
                client.DefaultRequestHeaders.Accept.Clear();
                
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
                   "Basic",
                    Convert.ToBase64String(
                       System.Text.ASCIIEncoding.ASCII.GetBytes(
                           string.Format("{0}:{1}", UserNameTextBox.Text, PasswordTextBox.Text))));

                // Like "v1/transactions/payments/accounts/A00000001?pageSize=40"
                HttpResponseMessage response = await client.GetAsync(APIURLRequest.Text);
                await ReturnJsonAsHTML(response);

            }
        }

protected void GoButton_Click(object sender, EventArgs e) 
    { 
        int idx = APIStyleDropBox.SelectedIndex; 
        ... deleted ... 
            else if (idx == 2) // Zuora JSON 
            { 
                  RegisterAsyncTask(new PageAsyncTask(TestZuoraJSON)); 
             }.
             ... deleted.... 
        }
    

 

Senior Software Engineer
Healthicity / AAPC
Zuora Support Moderator

Re: Basic authentication fails

Thanks for sharing! This will be super helpful for anyone else that wanders a similar path Smiley Happy



If you found my answer helpful, please give me a kudo ↑
Help others find answers faster by accepting my post as a solution √