How to apply credentials in PTV C#.JSON clients

Proper forum for all questions related to xServer INTERNET = the Azure based cloud solution. This forum deals with the architecture topics such as load balancing, available profiles and standard server settings.
Please be aware that questions about core functionality such as routing, mapping, geocoding in general should be placed in the xServers dedicated forum ;-)
Post Reply
User avatar
Bernd Welter
Site Admin
Posts: 2572
Joined: Mon Apr 14, 2014 10:28 am
Contact:

How to apply credentials in PTV C#.JSON clients

Post by Bernd Welter »

Cheerio,

as some of you might have noticed: we now also provide C# Client classes which are using JSON as message protocol for the exchange with PTV xServer2. Though we added a generic description into the Developers Guide I was missing the specific sample for the application of credentials. I already requested the chapter from DEV.

In the meantime check this piece of code to gather how to apply the credentials. The variable xtok is of course needed to be assigned first.

Code: Select all

IXServer xServerClient = new XServer(new Uri("https://xserver2-test.cloud.ptvgroup.com"), new HttpClientHandler { 
   Credentials = new System.Net.NetworkCredential { 
       UserName = "xtok",
       Password = xtok
   }
});;
Best regards,
Bernd
Bernd Welter
Technical Partner Manager Developer Components
PTV Logistics - Germany

Bernd at... The Forum,LinkedIn, Youtube, StackOverflow
I like the smell of PTV Developer in the morning... :twisted:
User avatar
Bernd Welter
Site Admin
Posts: 2572
Joined: Mon Apr 14, 2014 10:28 am
Contact:

Re: How to apply credentials in PTV C#.JSON clients

Post by Bernd Welter »

it would have been too easy... here's some annoying info from Ralf - who commented on my recommended piece of code:
yes I found that, too, but there's is still a disadvantage: All requests are sent twice, one time without the basic auth header (responded by http 401), a second time with the basic auth header (responded by http 200). Even if I set PreAuthenticate = true for the HttpClientHandler some requests are still sent twice.

Proposed alternative solution which always sends the basic auth header is:

Code: Select all

var encoded = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes("xtok:" + xtok));

XServer xServerClient = new XServer(new Uri("https://xserver2-test.cloud.ptvgroup.com"));

xServerClient.HttpClient.DefaultRequestHeaders.Authorization 
= new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", encoded);
Bernd Welter
Technical Partner Manager Developer Components
PTV Logistics - Germany

Bernd at... The Forum,LinkedIn, Youtube, StackOverflow
I like the smell of PTV Developer in the morning... :twisted:
Post Reply