I am trying to connect to a WebService that requires Windows Authentication using a Portable Class Library. I've looked around but so far I still didn't figure out a way to do that.
Here's what I've already tried:
Basic authentication on headers
WPService.WPTestSoapClient myclient = new WPService.WPTestSoapClient();
myclient.WP2_GetEncomendasClienteCompleted += myclient_WP2_GetEncomendasClienteCompleted;
using (OperationContextScope contextScope = new OperationContextScope(myclient.InnerChannel))
{
HttpRequestMessageProperty reqMessage = new HttpRequestMessageProperty();
reqMessage.Headers[System.Net.HttpRequestHeader.Authorization] = "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes("Domain\\User:Password"));
OperationContext.Current.OutgoingMessageProperties.Add(HttpRequestMessageProperty.Name, reqMessage);
myclient.WP2_GetEncomendasClienteAsync(cliente.Id);
}
The problem about this method is that it requires basic authentication and, therefore, not applicable to my current requirements
- Microsoft.Net.Http
I found this library which actually allows me to use NetworkCredentials and successfully authenticate on the service. The problem is, I have no idea how to use it, since when I call the code it returns the definition of the Webservice by default (the xml file), and I didn't find anywhere how to use it to call a method on a given Webservice.
The code I've done so far is:
var credentials = new NetworkCredential("user", "password", "domain");
var handler = new HttpClientHandler { Credentials = credentials };
using (var httpClient = new HttpClient(handler))
{
var content = new FormUrlEncodedContent(new[] {new KeyValuePair<string, string>("", "")});
content.Headers.ContentType = new MediaTypeHeaderValue("text/xml");
var response = await httpClient.PostAsync("http://ServiceURL", content);
var responseBodyAsText = await response.Content.ReadAsStringAsync();
return responseBodyAsText;
}
- Trying to set the NetworkCredentials on the service reference
No obvious way to do it, and it seems impossible to do
I would definitely appreciate help on this topic, and thanks in advance for looking at my question!