自托管WCF服务器和SSL

时间:2010-06-16 10:28:07

标签: c# wcf ssl

有自托管的WCF服务器(非IIS),并使用命令行生成证书(在Win Xp上),如

 makecert.exe -sr CurrentUser -ss My -a sha1 -n CN=SecureClient -sky exchange -pe
 makecert.exe -sr CurrentUser -ss My -a sha1 -n CN=SecureServer -sky exchange -pe

这些证书已添加到服务器代码中,如下所示:

serviceCred.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine,
                            StoreName.My, X509FindType.FindBySubjectName, "SecureServer");



serviceCred.ClientCertificate.SetCertificate(StoreLocation.LocalMachine,
                            StoreName.My, X509FindType.FindBySubjectName, "SecureClient");

在上一次操作之后,我创建了一个简单的客户端来检查与服务器的SSL连接。

客户端配置:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IAdminContract" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="TransportCredentialOnly">
                      <transport clientCredentialType="Basic"/>
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="https://myhost:8002/Admin" binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_IAdminContract" contract="Admin.IAdminContract"
                name="BasicHttpBinding_IAdminContract" />
        </client>
    </system.serviceModel>
</configuration>

代码:

Admin.AdminContractClient client = new AdminContractClient("BasicHttpBinding_IAdminContract");
            client.ClientCredentials.UserName.UserName = "user";
            client.ClientCredentials.UserName.Password = "pass";
            var result = client.ExecuteMethod()

执行期间我收到下一个错误:

  The provided URI scheme 'https' is invalid; expected 'http'.\r\nParameter name: via

问题:如何为自托管服务器启用ssl,我应该在哪里为客户端和服务器设置证书? 感谢。

1 个答案:

答案 0 :(得分:7)

尝试更改

<security mode="TransportCredentialOnly">

<security mode="Transport">

如果有任何改进,请告诉我们。这应该使您的客户端允许HTTPS连接。