Aus*_*tin 1 https wcf 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>
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
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
Run Code Online (Sandbox Code Playgroud)
我得到了这个例外:
{"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)在在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 []出)
找到解决方案:基本上,问题是标头必须附加到当前请求,如下所示:
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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6510 次 |
| 最近记录: |