gpt4 book ai didi

xml - 使用 WebHttpBinding 设置为基本身份验证的 WCF 进行 POX REST 调用时双重请求

转载 作者:数据小太阳 更新时间:2023-10-29 02:52:35 24 4
gpt4 key购买 nike

在将 WebHttpBinding 设置为基本身份验证 (HttpClientCredentialType.Basic) 的情况下使用 WCF 进行 POX REST 调用时出现问题

不是从具有 HTTP header 中指定的“Authorization: Basic”的客户端调用一次,而是进行两次调用。第一次调用完全没有身份验证,服务响应 401 Unauthorized 错误,第二次调用使用正确的身份验证信息。

这似乎是由 WCF 服务处理的,完全没有问题。调用第三方服务显然会产生问题,因为它们会立即响应错误。

服务代码:

[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,我看到这些背靠背的请求:

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>

注意只有第二次调用包含:Authorization: Basic dGVzdDp0ZXN0如何停止第一个请求(未经授权)?

可在此处下载带有 TcpTrace 实用程序的示例解决方案:

WCF-BasicAuthenticationIssue.zip

最佳答案

所以根据 Remus 的回答,这是我的解决方法

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

我只是强制第一个请求使用基本授权

关于xml - 使用 WebHttpBinding 设置为基本身份验证的 WCF 进行 POX REST 调用时双重请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1462256/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com