提问人:Zain Abbas 提问时间:10/30/2023 最后编辑:Zain Abbas 更新时间:10/31/2023 访问量:41
将 wsdl 与基本身份验证 .Net 配合使用
Use wsdl with basic authentication .Net
问:
我有一个受基本身份验证保护的 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"'.
帮助将不胜感激
答:
0赞
Zain Abbas
10/31/2023
#1
以下是这个 [链接][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
评论