我通过https连接到wcf服务有问题。我没有创建wcf服务。在Internet Explorer中,当我设置url end打开页面时,请让我写用户名和密码。
我打开了新的C#项目,想要添加新的服务引用,但每次出错:
下载' https://address/path/service.svc/_vti_bin/ListData.svc/ $元数据'时出错。 底层连接已关闭:发送时发生意外错误。 无法从传输连接读取数据:远程主机强制关闭现有连接。 远程主机强制关闭现有连接 元数据包含无法解析的引用:' https://address/path/service.svc.svc'。 向https://address/path/service.svc.svc发出HTTP请求时发生错误。这可能是由于在HTTPS情况下未使用HTTP.SYS正确配置服务器证书。这也可能是由客户端和服务器之间的安全绑定不匹配引起的。 底层连接已关闭:发送时发生意外错误。 无法从传输连接读取数据:远程主机强制关闭现有连接。 远程主机强制关闭现有连接 如果在当前解决方案中定义了服务,请尝试构建解决方案并再次添加服务引用。
我还尝试下载wsdl文件,并在添加服务引用时添加了本地wsdl文件。它通过但现在当我开始客户端时我得到了例外:
未提供用户名。在ClientCredentials中指定用户名。
try
{
ServiceReference1.IService stub = new ServiceReference1.ServiceClient();
stub.calculate("test");
}
catch (Exception ee)
{
}
但我在stub对象???
中没有ClientCredentials的选项的app.config
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/></startup>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService">
<security mode="Transport">
<transport clientCredentialType="Basic" proxyCredentialType="Basic" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https://address/path/service.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService"
contract="ServiceReference1.IService" name="BasicHttpBinding_IService" />
</client>
</system.serviceModel>
我该如何解决这个问题?
答案 0 :(得分:1)
ServiceClient
有一个名为ClientCredentials
的属性,但IService
没有。也许尝试将变量类型从IService
更改为ServiceClient
?
ServiceReference1.ServiceClient stub = new ServiceReference1.ServiceClient();
stub.ClientCredentials.UserName.UserName = username;
stub.ClientCredentials.UserName.Password = password;
// Treat the test certificate as trusted
stub.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.PeerOrChainTrust;
以下是MSDN的链接,其中显示了如何使用用户名/密码进行身份验证
答案 1 :(得分:1)
我在调用方法前放了:
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;
代码:
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3; //System.Net.ServicePointManager.
try
{
var stub = new ServiceReference1.ServiceClient();
stub.ClientCredentials.UserName.UserName = "user";
stub.ClientCredentials.UserName.Password = "pass";
//stub.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.PeerOrChainTrust;
//System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;
stub.calculate("Test");
}
catch (Exception ee)
{
}