为什么基本身份验证无法与我的Java SOAP Web服务的WCF客户端一起使用? [英] Why would Basic Auth not work with my WCF client to Java SOAP Web Service?
问题描述
我有一个基于Java的Web服务,该服务需要基本身份验证才能与其通信.如果我在浏览器中输入WSDL URL,则会提示我输入基本身份验证.输入正确的凭据即可获取.
I have a Java based web service that requires basic authentication to communicate with it. If I type the WSDL url into my browser I'm prompted for Basic Auth. Which I can get by entering the correct credentials.
但是无法使用WCF客户端.
However using my WCF client doesn't work.
我像这样构造我的WCF客户端:
I construct my WCF client like this:
var binding = new BasicHttpBinding
{
MaxReceivedMessageSize = 2048 * 10240,
Security = {
Mode = BasicHttpSecurityMode.TransportCredentialOnly,
Transport = {
ClientCredentialType = HttpClientCredentialType.Basic,
Realm = "MYREALM",
ProxyCredentialType = HttpProxyCredentialType.None
},
Message = {
ClientCredentialType = BasicHttpMessageCredentialType.UserName,
AlgorithmSuite = SecurityAlgorithmSuite.Default
}
}
};
var client = new WebServiceClient(binding, endpoint);
client.ClientCredentials.UserName.UserName = username;
client.ClientCredentials.UserName.Password = password;
client.DoWebServiceMethod();
我收到以下异常.
System.Net.WebException: The remote server returned an error: (401) Unauthorized.
at System.Net.HttpWebRequest.GetResponse()
at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
System.ServiceModel.Security.MessageSecurityException: The HTTP request is unauthorized with client authentication scheme 'Basic'. The authentication header received from the server was 'Basic realm="MYREALM"'.
据我所知,我做对了.我要去哪里错了?
From what I can tell I'm doing things right. Where am I going wrong?
推荐答案
我只是设置了同一件事-我在Visual Studio中添加了一个Service Reference,它的出现类似于您在代码中所做的事情.
I just setup the same thing -- I added a Service Reference in Visual Studio, and it came out similar to what you've done in code.
尽管有两个注释,尽管它已正确设置了安全模式="Transport",但未设置clientCredentialType ="Basic".我将其添加到该配置中,但仍然无法正常工作.然后我实际上删除了消息安全性,因为我联系的服务仅是SSL + Basic:
Two notes though, although it had security mode="Transport" set correctly, it didn't have clientCredentialType="Basic" set. I added to that my config and it still didn't work. Then I actually removed the message security since the service I'm contacting is SSL + Basic only:
<message clientCredentialType="UserName" algorithmSuite="Default" />
Voila-奏效了.
Voila -- it worked.
考虑到元素未指定消息级别的安全性,但我确实不确定为什么会产生效果.
I'm not sure why this had an effect considering the element did not specify message level security... but it did.
这篇关于为什么基本身份验证无法与我的Java SOAP Web服务的WCF客户端一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!