c# - 使用证书保护 IIS 中的 WCF 服务

标签 c# wcf security wshttpbinding

我想使用自签名证书(由 inetmgr 生成)保护 WCF 4 中的服务应用程序。

但是,我不能。当我调用服务的方法时,出现 MessageSecurityException:

The HTTP request was forbidden with client authentication scheme 'Anonymous'.

web.config 文件:

<?xml version="1.0"?>
<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
        <customErrors mode="Off"/>
    </system.web>

    <system.serviceModel>
        <bindings>
            <wsHttpBinding>
                <binding name="TransportSecurity">
                    <security mode="TransportWithMessageCredential">
                        <transport clientCredentialType="Certificate" />
                        <message clientCredentialType="Certificate"/>
                    </security>
                </binding>
            </wsHttpBinding>
        </bindings>

        <behaviors>
            <serviceBehaviors>
                <behavior name="testingServiceBehavior">
                    <serviceMetadata httpsGetEnabled="true" httpGetEnabled="false" />
                    <serviceDebug includeExceptionDetailInFaults="false"/>
                </behavior>
            </serviceBehaviors>
        </behaviors>

        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

        <services>
            <service behaviorConfiguration="testingServiceBehavior"
                     name="Testing.Service1">

                <endpoint address=""
                          binding="wsHttpBinding"
                          bindingConfiguration="TransportSecurity"
                          contract="Testing.IService1" />

                <endpoint address="mex"
                          binding="mexHttpsBinding"
                          contract="IMetadataExchange" />
            </service>
        </services>
    </system.serviceModel>

    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
    </system.webServer>
</configuration>

我尝试使用该服务的代码是:

    public static bool validateCertificates(object sender,
                                            System.Security.Cryptography.X509Certificates.X509Certificate cert,
                                            System.Security.Cryptography.X509Certificates.X509Chain chain,
                                            System.Net.Security.SslPolicyErrors error)
    {
        return true;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        System.Net.ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(validateCertificates);

        WSHttpBinding binding = new WSHttpBinding();
        binding.Name = "secureBinding";

        binding.Security.Mode = SecurityMode.TransportWithMessageCredential;
        binding.Security.Message.ClientCredentialType = MessageCredentialType.Certificate;
        binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;

        EndpointAddress endpointAddress = new System.ServiceModel.EndpointAddress("https://rtsa.dnsalias.com:2490/Service1.svc");

        ProCell2.Servicios.Informes.Service1Client client = new Servicios.Informes.Service1Client(binding, endpointAddress);

        client.ClientCredentials.ClientCertificate.SetCertificate(
                StoreLocation.CurrentUser,
                StoreName.My,
                X509FindType.FindBySubjectName,
                "ServerWeb2");

        client.ClientCredentials.ServiceCertificate.SetDefaultCertificate(
                StoreLocation.CurrentUser,
                StoreName.My,
                X509FindType.FindBySubjectName,
                "ServerWeb2");

        client.GetInformation();  // <-------- Here cause the exception

SSL 配置:

SSL Setting

最佳答案

请将以下几行添加到您的客户端代码中:

// Disable credential negotiation and the establishment of 
// a security context.
myBinding.Security.Message.NegotiateServiceCredential = false;
myBinding.Security.Message.EstablishSecurityContext = false;

参见 http://msdn.microsoft.com/en-us/library/ms733102.aspx有关详细信息,请参阅 What are the impacts of setting establishSecurityContext="False" if i use https?了解它对您的沟通的影响。

关于c# - 使用证书保护 IIS 中的 WCF 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7449005/

相关文章:

c# - 无法从 main 返回值

c# - 为什么我在编码网站上看到的大多数 DataContract 的 DataMembers 不是使用自动属性编写的?

没有 SSL 但具有 Windows 组身份验证的 WCF 服务

wcf - 读取命名空间时发生 ReadAsDataContract 异常

php - 在移动服务器通信中使用密码作为加密 key 的一部分

c# - 使用 ASP .NET 表单例份验证,如果新的身份验证 cookie 被盗,它是否可以在另一台 PC 上使用?

c# - 在这种情况下,什么可以解释使用 const 的开销?

c# - double z=x-y 是否保证 IEEE 754 float 的 z+y==x?

c# - .NET 和 MySQL 错误 - 调用 SSPI 失败 ... "message received was unexpected or badly formatted"和 "buffers supplied to a function was too small"

asp.net-mvc - 防止更改隐藏字段