Double request while making POX REST call using WCF with WebHttpBinding set to Basic Authentication

前端 未结 2 1994
借酒劲吻你
借酒劲吻你 2021-01-15 16:24

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

相关标签:
2条回答
  • 2021-01-15 16:30

    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.

    0 讨论(0)
  • 2021-01-15 16:43

    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

    0 讨论(0)
提交回复
热议问题