【发布时间】:2019-09-19 19:38:42
【问题描述】:
我需要为每个使用 HTTPS 端点发送到项目中的连接服务之一的有效负载添加自定义标头。该自定义标头将用作接收者的身份验证。我该怎么做?
我尝试使用System.ServiceModel.Channels.MessageHeader 和AddressHeader,但没有成功。使用 SoapUI,向请求中添加自定义标头可以正常工作。
这种身份验证方法对接收方来说是新的。以及 HTTPS 端点。在以前的请求中,我使用带有用户名和密码的Basic Http Authentication。因此,我对此进行了一些更改以支持 HTTPS 绑定代码:
BasicHttpsBinding basicAuthBinding = new BasicHttpsBinding(BasicHttpsSecurityMode.Transport);
basicAuthBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
EndpointAddress basicAuthEndpoint = new EndpointAddress("https://whatever");
MyConnectedService.MyOperationClient client = new MyConnectedService.MyOperationClient(basicAuthBinding, basicAuthEndpoint);
client.ClientCredentials.UserName.UserName = "user";
client.ClientCredentials.UserName.Password = "pass";
client.send(payload);
我相信在这里的某个地方我需要添加自定义标题。
通过这段代码,我得到了预期的HTTP 401 - Unauthorized。
帮助表示赞赏。
提前谢谢你。
【问题讨论】: