J'ai un service Web qui nécessite une authentification de base. Je l'ai testé avec un petit programme Java.Authentification de base au service Web avec C#
essentiellement:
...
String authorization = s + ':' + (s1 != null ? s1 : "");
authorization = Base64.getEncoder().encodeToString(authorization2.getBytes());
httpurlconnection.setRequestProperty("Authorization", "Basic " + authorization);
....
Cela fonctionne très bien. Mais je dois gérer cela avec un programme C#. J'ajoute un "Service Reference" dans mon projet en important le fichier wsdl. , je pense Après beaucoup de recherche ce serait l'affaire:
WSHttpBinding binding = new WSHttpBinding();
binding.Name = "pisconfigwebserviceSOAP";
EndpointAddress epAdd = new EndpointAddress(remoteAddress);
myWebserviceClient client = new myWebserviceClient(binding, epAdd);
ContractDescription cd = ContractDescription.GetContract(typeof(myWebservice), typeof(myWebserviceClient));
client.Endpoint.Contract = cd;
// this part should add the Basic Authentication to the header. Or not?
using (OperationContextScope scope = new OperationContextScope(client.InnerChannel)) {
var httpRequestProperty = new HttpRequestMessageProperty();
httpRequestProperty.Headers.Add(HttpRequestHeader.Authorization, "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(mUserName + ":" + mPassword)));
OperationContext.Current.OutgoingMessageProperties.Add(HttpRequestMessageProperty.Name, httpRequestProperty);
int result = client.AddOrUpdate(obj);
}
Je ne sais pas ce que je fais mal, essayer beaucoup de choses différentes et je suis ici en tiens. J'apprécierais toute aide. Merci