【问题标题】:WCF Client - connect to serviceWCF 客户端 - 连接到服务
【发布时间】:2017-05-15 17:37:19
【问题描述】:

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

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

下载“https://address/path/service.svc/_vti_bin/ListData.svc/$metadata”时出错。 底层连接已关闭:发送时发生意外错误。 无法从传输连接读取数据:现有连接被远程主机强行关闭。 现有连接被远程主机强行关闭 元数据包含无法解析的引用:“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)
{
}

但我在存根对象中没有 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>

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

【问题讨论】:

    标签: c# wcf ssl https client


    【解决方案1】:

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

    【讨论】:

    • 谢谢,现在我可以选择凭据,但现在调用方法时出现错误:向address/path/Service.svc 发出 HTTP 请求时出错。这可能是由于在 HTTPS 情况下未使用 HTTP.SYS 正确配置服务器证书。这也可能是由于客户端和服务器之间的安全绑定不匹配造成的。
    • 有几个很好的指针here。如果不查看证书详细信息,很难更具体。另外,您可以尝试Fiddler 看看您是否可以获得有关错误的更多详细信息,以及尝试捕获内部异常
    • 谢谢你:
    【解决方案2】:

    我在调用方法之前放:

                    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)
            {
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-27
      • 2013-02-22
      • 2023-03-09
      • 1970-01-01
      • 2010-11-17
      • 2023-03-09
      • 2010-09-22
      相关资源
      最近更新 更多