我正在开发一个应用程序,在该应用程序中,我必须使用使用http协议的Java开发的where服务。我正在使用C#.NET winforms开发应用程序。到现在为止一切都很顺利。webservice现在正在使用SSL安全性,因此服务协议从http更改为https。在访问https the服务时,我遇到了一些问题。
通过从Auth选项卡提供身份验证参数(UserName和密码),我尝试从UserName访问https tab服务,如下所示:

它从SoapUI那里运行得很好。
但是,我从代码中提供了身份验证参数,如下所示:
client.ClientCredentials.UserName.UserName = "admin";
client.ClientCredentials.UserName.Password = "*******";我使用安全模式as:TransportWithMessageCredential和ClientCredentialTtype as:Basic
我的App.Config文件如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<client>
<endpoint address="https://xyz:8001/HelloWorldAPI/HelloWorldWebService"
binding="wsHttpBinding"
bindingConfiguration="myhttpsbinding" contract="API.HelloWorldWebService"
name="HelloWorldWebServicePort" />
</client>
<bindings>
<wsHttpBinding>
<binding name="myhttpsbinding">
<security mode="TransportWithMessageCredential">
<transport clientCredentialType="Basic"/>
</security>
</binding>
</wsHttpBinding>
</bindings>
</system.serviceModel>
<system.net>
<defaultProxy useDefaultCredentials="true" />
</system.net>
</configuration>我的守则如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using testingtool.API;
namespace testingtool
{
class Program
{
static void Main(string[] args)
{
new APITool();
}
}
class APITool
{
UserInfo userinfo = new UserInfo();
HelloWorldWebServiceClient client = new HelloWorldWebServiceClient();
private bool ValidationCallBack(object sender, X509Certificate cert, X509Chain chain, System.Net.Security.SslPolicyErrors error)
{
return true;
}
public APITool()
{
try
{
//Authentication parameters
client.ClientCredentials.UserName.UserName = "admin";
client.ClientCredentials.UserName.Password = "*****";
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(ValidationCallBack);
//client ClientCredentials @ Application level
userinfo.userid = "myusername";
userinfo.password = "*****";
GetHelloWorldAPIVersionRequest request = new GetHelloWorldAPIVersionRequest();
APIVersionInfo versioninfo = new APIVersionInfo();
versioninfo.userinfo = userinfo;
request.APIVersionInfo = versioninfo;
APIVersionInfoResponse response = client.GetHelloWorldAPIVersion(versioninfo);
Console.WriteLine(response.Version);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.ReadLine();
}
}
} 我得到了以下例外:
System.ServiceModel.Security.MessageSecurityException:请求是未经授权的客户端身份验证方案‘匿名’。从服务器接收到的身份验证标头是“基本realm=”“EJBServiceEndpointServlet领域”。-> System.Net.WebException:远程服务器返回一个错误:(401)未经授权。
编辑:来自客户端的,我已经用Fiddler验证了请求窗口,如下所示:从AUth选项卡中,它表示不存在自动化头。

Fiddler原始请求如下:
CONNECT 10.10.10.110:8001
HTTP/1.1 Host: 10.10.10.110:8001
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)任何帮助都会受到极大的感谢。
发布于 2014-06-17 11:48:32
想知道这个问题是否与绑定有关,但如果不检查服务配置或查看成功的soapUI请求,就很难确定。(注意:您可能希望包含来自soapUI HTTP的消息片段,包括soap头。)
无论如何,您可能希望通过尝试以下绑定来确保服务真正使用ws-security (消息级安全性):
<bindings>
<basicHttpBinding>
<binding name="httpBinding">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Basic" />
</security>
</binding>
</basicHttpBinding>
</bindings>发布于 2014-06-16 09:10:57
您应该更改应用程序配置:
<wsHttpBinding>
<binding name="myhttpsbinding">
<security mode="TransportWithMessageCredential">
<transport clientCredentialType="None" />
<message clientCredentialType="UserName" establishSecurityContext="false" negotiateServiceCredential="false"/>
</security>
</binding>
</wsHttpBinding>在传输级别上,您没有身份验证,所以<transport clientCredentialType="None" />和消息级别上您都有用户名\密码身份验证,所以是<message clientCredentialType="UserName"
发布于 2014-06-17 12:12:13
你要通过代理服务器吗?如果是,是否将详细信息输入IE?你能在IE中浏览https页面吗?如果是的话,每个进行https调用的WebRequest都应该获取它,因为它需要向代理服务器发出一个CONNECT (它是这样做的),但是在您的调用中缺少Proxy-Authorization报头。这应该是您关注的焦点--尝试一个普通的WebRequest来表示https:// google.com,然后看看您在fiddler中得到了什么。
您的代理服务器在您的请求中转发可能会出现问题。您能在不同的网络上而不是在代理后面尝试http吗?
https://stackoverflow.com/questions/24202633
复制相似问题