【问题标题】:Secure WCF service in IIS using certificates使用证书在 IIS 中保护 WCF 服务
【发布时间】:2011-09-16 18:39:46
【问题描述】:

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

但是,我不能。当我调用服务的方法时,我有一个 MessageSecurityException:

HTTP 请求被客户端身份验证方案“匿名”禁止。

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 配置:

【问题讨论】:

  • 从哪个版本的 IIS 运行?启用 SSL 了吗?
  • 在iis 7.0.6下运行,并且开启了ssl,iis中的绑定具有相同的证书。

标签: c# wcf security wshttpbinding


【解决方案1】:

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

// 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? 了解它对您的沟通的影响。

【讨论】:

  • 感谢您的帮助,但仍然是同样的错误。甚至,我把它放在 web.config 中试试。
  • 如果您执行msdn.microsoft.com/en-us/library/ms733102.aspx 中提供的确切示例,您还会遇到相同的错误吗?显然,问题出在您的客户端,因此需要更改客户端代码。或者,另一种可能性;您尝试登录的证书是否在服务器端的受信任根权限列表中?否则,您的质询将不包含该证书根,并且您的客户端将无法将其与请求一起发送。
猜你喜欢
  • 1970-01-01
  • 2011-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-11
  • 1970-01-01
  • 2014-03-28
相关资源
最近更新 更多