Having an issue while making POX REST call using WCF with WebHttpBinding set to Basic Authentication (HttpClientCredentialType.Basic)
Instead of one call from the cl
The problem actually lies with those third parties. Per RFC 2617 Basic authentication is done in two calls, just like Digest. The server should respond at the first call with a challenge containing a realm:
Upon receipt of an unauthorized request for a URI within the
protection space, the origin server MAY respond with a challenge like
the following:WWW-Authenticate: Basic realm="WallyWorld"
where "WallyWorld" is the string assigned by the server to identify
the protection space of the Request-URI
The WCF endpoint will pre-authenticate only subsequent calls after the first one. The very first call made by an appdomain to any resource will not contain the basic header user name and password.
See also What happened to the PreAuthenticate flag in WCF.
So based on Remus's answer this is my workaround
public Message SendData(Message requestMessage)
{
var channel = CreateChannel();
Message responseMessage;
using (new OperationContextScope((IClientChannel)channel))
{
WebOperationContext.Current.OutgoingRequest
.Headers[HttpRequestHeader.Authorization] = "Basic "
+ Convert.ToBase64String(Encoding.ASCII.GetBytes(
Credentials.UserName.UserName + ":" + Credentials.UserName.Password));
responseMessage = channel.SendData(requestMessage);
}
return responseMessage;
}
I'm simply forcing first request to go out with Basic Authorization