c# - WCF 客户端 - 连接到服务

标签 c# wcf ssl https client

我在通过 https 连接到 wcf 服务时遇到问题。我没有创建 wcf 服务。在 Internet Explorer 中,当我设置 url 结束打开页面时,要求我输入用户名和密码。

我打开了一个新的 C# 项目,想添加新的服务引用,但每次我都得到错误:

There was an error downloading 'https://address/path/service.svc/_vti_bin/ListData.svc/$metadata'. The underlying connection was closed: An unexpected error occurred on a send. Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. An existing connection was forcibly closed by the remote host Metadata contains a reference that cannot be resolved: 'https://address/path/service.svc.svc'. An error occurred while making the HTTP request to https://address/path/service.svc.svc. This could be due to the fact that the server certificate is not configured properly with HTTP.SYS in the HTTPS case. This could also be caused by a mismatch of the security binding between the client and the server. The underlying connection was closed: An unexpected error occurred on a send. Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. An existing connection was forcibly closed by the remote host If the service is defined in the current solution, try building the solution and adding the service reference again.

我还尝试下载 wsdl 文件,并通过添加服务引用添加了这个本地 wsdl 文件。它通过了,但是现在当我启动客户端时出现异常:

The username is not provided. Specify username in ClientCredentials.

try
{
    ServiceReference1.IService stub = new ServiceReference1.ServiceClient();
    stub.calculate("test");
}
catch (Exception ee)
{
}

但我在 stub 对象中没有 ClientCredentials 选项???

应用程序配置

<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>

我该如何解决这个问题??

最佳答案

ServiceClient definition有一个名为 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 的链接显示如何使用用户名/密码进行身份验证

关于c# - WCF 客户端 - 连接到服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43985622/

相关文章:

c# - 'application configuration' 文件中的最大键/值长度

c# - WPF 创建全局 WinSCP session

c# - 使用 Web 服务更新 Dynamics CRM 中的 StateCode 失败

c# - 如何获取 WCF 服务的监听地址/端口?

c# - SqlDataAdapter C# WCF 服务

php - fsockopen() 无法连接到 ssl

c# - 如何检查列是否可以转换为 double ? (DataTable.Column.DataType)

.net - IIS : web applications warmup

postgresql - 使用 Cloud Shell 和 SSL 连接到 Google Cloud SQL PostgreSQL?

java - 收到致命警报:SSLHandshakeException中的handshake_failure