使用 WCF 进行 POX REST 调用时出现双重请求,WebHttpBinding 设置为基本身份验证 [英] Double request while making POX REST call using WCF with WebHttpBinding set to Basic Authentication
问题描述
在将 WebHttpBinding 设置为基本身份验证 (HttpClientCredentialType.Basic) 的情况下,使用 WCF 进行 POX REST 调用时出现问题
Having an issue while making POX REST call using WCF with WebHttpBinding set to Basic Authentication (HttpClientCredentialType.Basic)
不是来自客户端的一次调用,并在 HTTP 标头中指定了授权:基本",而是进行了两次调用.第一次调用完全没有身份验证,服务响应 401 Unauthorized 错误,第二次调用使用正确的身份验证信息.
Instead of one call from the client with "Authorization: Basic" specified in HTTP Header, two calls are made. First call without authentication at all to which service responses with 401 Unauthorized error, second call with proper authentication info.
这似乎是由 WCF 服务处理的,没有任何问题.调用第三方服务显然会产生问题,因为它们会立即响应错误.
This seems to be handled by WCF service with no hiccup at all. Calling third party services obviously creates a problem since they respond with error immediately.
服务代码:
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Xml,
UriTemplate = "")]
Message SendData(Message message);
}
public class Service : IService
{
public Message SendData(Message message)
{ return Message.CreateMessage(MessageVersion.None, String.Empty, "test");
}
}
客户代码:
public class Client: WebChannelFactory<IService>, IService
{
public Client(Uri baseUri, string userName, string password)
: base(CreateBinding(),
baseUri)
{
Credentials.UserName.UserName = userName;
Credentials.UserName.Password = password;
}
public Message SendData(Message requestMessage)
{
var channel = CreateChannel();
Message responseMessage = channel.SendData(requestMessage);
return responseMessage;
}
private static Binding CreateBinding()
{
var binding = new WebHttpBinding();
binding.Security.Mode = WebHttpSecurityMode.TransportCredentialOnly;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
return binding;
}
}
使用 TcpTrace 我看到这些请求背靠背:
Using TcpTrace I’m seeing these to requests back to back:
POST / HTTP/1.1
Content-Type: application/xml; charset=utf-8
VsDebuggerCausalityData: uIDPo2lH6p+lUOdFmrqDKGWYeQkAAAAA7+Y4QR6wNUWZmwCaasMx7xrfcJZxph9NocstwCh8NQsACQAA
Host: localhost:9090
Content-Length: 89
Expect: 100-continue
Connection: Keep-Alive
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">test request</string>
POST / HTTP/1.1
Content-Type: application/xml; charset=utf-8
VsDebuggerCausalityData: uIDPo2lH6p+lUOdFmrqDKGWYeQkAAAAA7+Y4QR6wNUWZmwCaasMx7xrfcJZxph9NocstwCh8NQsACQAA
Authorization: Basic dGVzdDp0ZXN0
Host: localhost:9090
Content-Length: 89
Expect: 100-continue
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">test request</string>
注意仅第二个调用包含:授权:基本 dGVzdDp0ZXN0如何停止第一个请求(未经授权)?
Note only second call contains: Authorization: Basic dGVzdDp0ZXN0 How to stop the first request (without authorization) to be made?
可以在此处下载使用 TcpTrace 实用程序的示例解决方案:
Sample solution with TcpTrace utility can be downloaded here:
WCF-BasicAuthenticationIssue.zip
推荐答案
所以根据 Remus 的回答,这是我的解决方法
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
这篇关于使用 WCF 进行 POX REST 调用时出现双重请求,WebHttpBinding 设置为基本身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!