为了与 SOAP 网络服务进行通信,我从 wsdl 文件中创建了由 SvcUtil.exe 创建的 C# 类。
将下面的请求发送到安全服务器(带有 BASIC 身份验证的 HTTPS)时,我收到 System.ServiceModel.Security.MessageSecurityException 并且在通过 Burp 代理检查 HTTP 请求时,我看到没有传递 BASIC 身份验证信息。 C# 代码中的 SOAP 请求是否缺少任何内容,或者 BASIC 身份验证不起作用可能是什么问题?
var binding = new WSHttpBinding();
binding.MessageEncoding = WSMessageEncoding.Mtom;
binding.Security.Mode = SecurityMode.Transport;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
binding.Name = "BasicAuthSecured";
SearchServicePortClient searchClient = new SearchServicePortClient(binding, new EndpointAddress("https://myUrl:Port/myService"));
searchClient.ClientCredentials.UserName.UserName = "username";
searchClient.ClientCredentials.UserName.Password = "pw";
query soap = new query();
//...
queryResponse response = searchClient.query(soap);
提前致谢
最佳答案
这是另一种方法,不知道它是否是最好的
using (var client = _clientFactory.GetClient())
{
var credentials = Utils.EncodeTo64("user123:password");
client.ChannelFactory.CreateChannel();
using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
{
var httpRequestProperty = new HttpRequestMessageProperty();
httpRequestProperty.Headers[HttpRequestHeader.Authorization] = "Basic " + credentials;
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
//operation
client.Send(request);
}
}
关于c# - .NET SOAP 不在请求 header 中发送 BASIC 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43544002/