【问题标题】:Basic HTTP Authentication over HTTPS with WCF使用 WCF 通过 HTTPS 进行基本 HTTP 身份验证
【发布时间】:2012-01-30 17:21:06
【问题描述】:

我正在通过 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) Unauthorized."}

带有堆栈跟踪:

在 System.ServiceModel.Channels.HttpChannelUtilities.ValidateAuthentication(HttpWebRequest 请求,HttpWebResponse 响应,WebException responseException,HttpChannelFactory 工厂) 在 System.ServiceModel.Channels.HttpChannelUtilities.ValidateRequestReplyResponse(HttpWebRequest 请求,HttpWebResponse 响应,HttpChannelFactory 工厂,WebException responseException,ChannelBinding 通道绑定) 在 System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(时间跨度超时) 在 System.ServiceModel.Channels.RequestChannel.Request(消息消息,TimeSpan 超时) 在 System.ServiceModel.Dispatcher.RequestChannelBinder.Request(消息消息,TimeSpan 超时) 在 System.ServiceModel.Channels.ServiceChannel.Call(字符串操作,布尔单向,ProxyOperationRuntime 操作,Object[] 输入,Object[] 输出,TimeSpan 超时) 在 System.ServiceModel.Channels.ServiceChannel.Call(字符串操作,布尔单向,ProxyOperationRuntime 操作,Object[] 输入,Object[] 输出) 在 System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage 方法调用,ProxyOperationRuntime 操作) 在 System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage 消息)

【问题讨论】:

  • 您是否因为需要支持 SOAP 1.1 客户端而使用 basicHttpBinding 而不是 WSHttpBinding

标签: wcf https basic-authentication


【解决方案1】:

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

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

【讨论】:

  • string base_64_encoded_credentials = System.Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(client.ClientCredentials.UserName.UserName + ":" + client.ClientCredentials.UserName.Password));
【解决方案2】:

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

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

【讨论】:

  • 用 Fiddler 监听后,看起来根本不像在发送。难道是WCF在发送https请求之前没有附加HTTP授权头?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-17
  • 2017-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多