使用WCF通过HTTPS进行基本HTTP身份验证

时间:2011-12-30 17:44:40

标签: wcf https basic-authentication

我通过WCF(w / .NET 4.0)调用Web服务,该服务需要通过HTTPS进行基本HTTP身份验证(使用用户名和密码。)。虽然,我认为我已经正确设置了所有内容,但每当我打电话给服务时,我都会收到401错误。我监视了HTTP流量,并注意到WCF似乎忽略了我告诉它使用带有用户名和密码的授权标头,因为在请求中没有发送。这是我的配置如下。有任何想法吗?谢谢!

   <system.serviceModel>
         <bindings>
                <basicHttpBinding>
                       <binding name="BasicAuthSecured">
                             <security mode="Transport">
                                    <transport clientCredentialType="Basic" />
                             </security>
                       </binding>
                       </basicHttpBinding>
                </bindings>
         <client>
                <endpoint address="https://REMOVED FOR CONFIDENTIALITY"
            binding="basicHttpBinding" bindingConfiguration="BasicAuthSecured"
            contract="Five9.WsAdmin" name="WsAdminPort" />
         </client>
         <behaviors>
                <serviceBehaviors>
                       <behavior name="">
                             <serviceMetadata httpsGetEnabled="true"/>
                             <serviceDebug includeExceptionDetailInFaults="true"/>
                       </behavior>
                </serviceBehaviors>
         </behaviors>
   </system.serviceModel>

这是我的代码:

        var five_9_client = new WsAdminClient();

        five_9_client.ClientCredentials.UserName.UserName = “REMOVED FOR CONFIDENTIALITY";
        five_9_client.ClientCredentials.UserName.Password = "REMOVED FOR CONFIDENTIALITY";

        var call_log_response = five_9_client.getCallLogReport(call_log); //Bombing out here

我遇到了这个例外:

{“HTTP请求未经授权,客户端身份验证方案为”基本“。从服务器收到的身份验证标头为''。}}

内部异常:

{“远程服务器返回错误:(401)未经授权。”}

使用堆栈跟踪:

at System.ServiceModel.Channels.HttpChannelUtilities.ValidateAuthentication(HttpWebRequest request,HttpWebResponse response,WebException responseException,HttpChannelFactory factory)    在System.ServiceModel.Channels.HttpChannelUtilities.ValidateRequestReplyResponse(HttpWebRequest请求,HttpWebResponse响应,HttpChannelFactory工厂,WebException responseException,ChannelBinding channelBinding)    at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)    在System.ServiceModel.Channels.RequestChannel.Request(消息消息,TimeSpan超时)    在System.ServiceModel.Dispatcher.RequestChannelBinder.Request(消息消息,TimeSpan超时)    在System.ServiceModel.Channels.ServiceChannel.Call(String action,Boolean oneway,ProxyOperationRuntime operation,Object [] ins,Object [] outs,TimeSpan timeout)    在System.ServiceModel.Channels.ServiceChannel.Call(String action,Boolean oneway,ProxyOperationRuntime operation,Object [] ins,Object [] outs)    在System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall,ProxyOperationRuntime操作)    在System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

2 个答案:

答案 0 :(得分:5)

找到解决方案:基本上,问题是标题必须附加到当前请求,如下所示:

      var base_64_encoded_credentials =
            BasicHTTPAuthenticationEncoder.base_64_encode_credentials(
                client.ClientCredentials.UserName.UserName, client.ClientCredentials.UserName.Password);

        HttpRequestMessageProperty request = new HttpRequestMessageProperty();
        request.Headers[System.Net.HttpRequestHeader.Authorization] = "Basic " + base_64_encoded_credentials;

        OperationContextScope clientScope = new OperationContextScope(five_9_client.InnerChannel);
        OperationContext.Current.OutgoingMessageProperties.Add(HttpRequestMessageProperty.Name, request);

答案 1 :(得分:0)

可能问题是您尚未设置客户端凭据类型,请参阅http://msdn.microsoft.com/en-us/library/ms731925.aspx

根本没有发送,或者默认的客户端凭据类型不会在标头中发送凭据。