我有一个受基本身份验证保护的 wsdl,当我尝试使用邮递员或通过浏览器时它可以工作..
要在我的代码中使用它,我已通过 Visual Code 2022 连接该服务,并且已创建代理类..
然后我使用以下代码来使用它的方法
BasicHttpBinding binding = new BasicHttpBinding
{
SendTimeout = TimeSpan.FromSeconds(500),
MaxBufferSize = int.MaxValue,
MaxReceivedMessageSize = int.MaxValue,
AllowCookies = true,
ReaderQuotas = XmlDictionaryReaderQuotas.Max
};
binding.Security.Mode = BasicHttpSecurityMode.Transport;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
EndpointAddress address = new EndpointAddress("https://test/test/.wsdl");
ChannelFactory<TEST_Channel> factory = new ChannelFactory<TEST_Channel>(binding, address);
factory.Credentials.UserName.UserName = "abcd";
factory.Credentials.UserName.Password = "1234";
var f = factory.CreateChannel();
f.someMethod(param);
但是在调用 someMethod() 时出现以下错误
System.ServiceModel.Security.MessageSecurityException : The HTTP request is unauthorized with client authentication scheme 'Basic'. The authentication header received from the server was 'Digest realm="SOAP-Server", nonce="aasdasdasd1231313", algorithm="MD5", qop="auth"'.
如有帮助,我们将不胜感激
以下是此[链接][1]为我提供的解决方案
var client = new TEST_ChannelClient();
using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
{
var httpRequestProperty = new HttpRequestMessageProperty();
httpRequestProperty.Headers[System.Net.HttpRequestHeader.Authorization] = "Basic " +
Convert.ToBase64String(Encoding.ASCII.GetBytes("<userName>" + ":" +
"Password"));
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
var b = await client.someMethod();
}
[1]: https://stackoverflow.com/questions/39354562/consuming-web-service-with-c-sharp-and-basic-authentication/39357477#39357477