首页 文章

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

提问于
浏览
1

我通过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请求未经授权,客户端身份验证方案为'Basic' . 从服务器收到的身份验证标头为'' . ”}

内部异常:

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

使用堆栈跟踪:

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

2 回答

  • 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);
    
  • 0

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

    要么根本不发送,要么默认客户端凭据类型不在标头中发送凭据 .

相关问题