System.Security.Cryptography.CryptographicException:参数不正确

时间:2011-12-09 16:35:53

标签: c# wcf x509

尝试创建新的ServiceHost实例时,我遇到以下异常(新的ServiceHost(serviceType,baseAddresses))

System.Security.Cryptography.CryptographicException: The parameter is incorrect.

Stack Trace: 


[CryptographicException: The parameter is incorrect.
]
   System.Security.Cryptography.X509Certificates.X509Certificate2Collection.LoadStoreFromBlob(Byte[] rawData, String password, UInt32 dwFlags, Boolean persistKeyContainers) +1871625
   System.Security.Cryptography.X509Certificates.X509Certificate2Collection.Import(Byte[] rawData, String password, X509KeyStorageFlags keyStorageFlags) +104
   System.ServiceModel.Description.ConfigLoader.LoadIdentity(IdentityElement element) +501
   System.ServiceModel.Description.ConfigLoader.LoadServiceDescription(ServiceHostBase host, ServiceDescription description, ServiceElement serviceElement, Action`1 addBaseAddress) +12346988
   System.ServiceModel.ServiceHostBase.LoadConfigurationSectionInternal(ConfigLoader configLoader, ServiceDescription description, ServiceElement serviceSection) +67
   System.ServiceModel.ServiceHostBase.ApplyConfiguration() +108
   System.ServiceModel.ServiceHostBase.InitializeDescription(UriSchemeKeyedCollection baseAddresses) +192
   System.ServiceModel.ServiceHost.InitializeDescription(Type serviceType, UriSchemeKeyedCollection baseAddresses) +49
   System.ServiceModel.ServiceHost..ctor(Type serviceType, Uri[] baseAddresses) +151
   STARTAccessPoint.StartServiceFactory.CreateServiceHost(Type serviceType, Uri[] baseAddresses) in D:\Work\Projects\Marlo\LIME\projecto_start\start\project\START\STARTAccessPoint\accesspointService.svc.cs:273
   System.ServiceModel.Activation.ServiceHostFactory.CreateServiceHost(String constructorString, Uri[] baseAddresses) +422
   System.ServiceModel.HostingManager.CreateService(String normalizedVirtualPath) +1461
   System.ServiceModel.HostingManager.ActivateService(String normalizedVirtualPath) +44
   System.ServiceModel.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath) +651

[ServiceActivationException: The service '/accesspointService.svc' cannot be activated due to an exception during compilation.  The exception message is: The parameter is incorrect.
.]
   System.Runtime.AsyncResult.End(IAsyncResult result) +688590
   System.ServiceModel.Activation.HostedHttpRequestAsyncResult.End(IAsyncResult result) +190
   System.ServiceModel.Activation.ServiceHttpModule.EndProcessRequest(IAsyncResult ar) +310694
   System.Web.AsyncEventExecutionStep.OnAsyncEventCompletion(IAsyncResult ar) +94

以下是我使用的代码:

protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {
        if (!CertificatesConfiguration.HasServiceCredentials(serviceType.FullName))
        {
            throw new ConfigurationErrorsException(String.Format("Config section does not contain service credentials for service \"{0}\".", serviceType.FullName));
        }
        var credentials = CertificatesConfiguration.ServiceCredentials.ByServiceName(serviceType.FullName);

        //X509Certificate cert = credentials.ServiceCertificate.Certificate;

        return InitServiceHost(new ServiceHost(serviceType, baseAddresses),typeof(STARTAccessPoint.Resource),credentials.ServiceCertificate.Certificate);
    }

这是我的Web.Config:

<services>
  <service name="STARTAccessPoint.accesspointService" behaviorConfiguration="SecureServiceBehavior">
    <endpoint address="" name="ResourceBindingPort" binding="customBinding" bindingNamespace="http://www.w3.org/2009/02/ws-tra" bindingConfiguration="SecureServiceBinding" contract="STARTAccessPoint.Resource">
      <identity>
        <certificate encodedValue=""/>
      </identity>
    </endpoint>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="SecureServiceBehavior">
      <serviceMetadata httpsGetEnabled="true" externalMetadataLocation="https://localhost:1443/accesspointService.wsdl.xml"/>
      <serviceDebug includeExceptionDetailInFaults="true"/>
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="SecureClientBehavior">
    </behavior>
  </endpointBehaviors>
</behaviors>
<bindings>
  <customBinding>
    <binding name="SecureServiceBinding" closeTimeout="00:01:30" openTimeout="00:01:30" sendTimeout="00:01:30" receiveTimeout="00:01:30">
      <reliableSession ordered="true" reliableMessagingVersion="WSReliableMessaging11" flowControlEnabled="false" maxRetryCount="4" inactivityTimeout="00:01:00"/>
      <security defaultAlgorithmSuite="Basic128" allowSerializedSigningTokenOnReply="true" authenticationMode="MutualCertificate" requireDerivedKeys="false" securityHeaderLayout="Lax" messageProtectionOrder="SignBeforeEncrypt" messageSecurityVersion="WSSecurity10WSTrust13WSSecureConversation13WSSecurityPolicy12BasicSecurityProfile10" requireSignatureConfirmation="false">
        <issuedTokenParameters keyType="SymmetricKey"/>
        <secureConversationBootstrap/>
      </security>
      <httpsTransport authenticationScheme="Anonymous" hostNameComparisonMode="WeakWildcard" proxyAuthenticationScheme="Anonymous"/>
    </binding>
  </customBinding>
</bindings>

Cerificates配置文件,包含有关证书的所有信息:

<?xml version="1.0" encoding="UTF-8"?>
<peppol.certificates>
  <validation>
    <add name="MyRootCertificates"
         rootCACertificateThumbprint=""
         intermediateAcessPointCACertificateThumbprint="‎"/>
  </validation>

  <serviceCredentials>
    <add serviceName="SecureClient_at_localhost">
      <serviceCertificate
          findValue="‎7a6fa503ab57b81d6318a51ca265e739a51ce660"
          x509FindType="FindByThumbprint"
          storeName="Root"
          storeLocation="LocalMachine" />
    </add>
    <add serviceName="*">
      <serviceCertificate
          findValue="‎7a6fa503ab57b81d6318a51ca265e739a51ce660"
          x509FindType="FindByThumbprint"
          storeName="Root"
          storeLocation="LocalMachine" />
    </add>
  </serviceCredentials>
  <clientCredentials>
    <add endpointName="SecureClient_at_localhost">
      <clientCertificate
        filename="c:\localhost.cer"
        password="" />
      <serviceCertificate
         findValue="‎7a6fa503ab57b81d6318a51ca265e739a51ce660"
         x509FindType="FindByThumbprint"
         storeName="Root"
         storeLocation="LocalMachine" />
    </add>
    <add endpointName="*">
      <clientCertificate
        filename="c:\localhost.cer"
        password="" />
      <serviceCertificate
        filename="c:\localhost.cer"
        password="" />
    </add>
  </clientCredentials>
</peppol.certificates>

你能帮忙吗?

2 个答案:

答案 0 :(得分:2)

根据错误消息,配置文件中引用的证书不正确。

Microsoft的

Here is a good set of instructions有关如何配置和使用证书的信息。

答案 1 :(得分:0)

我确信这并不总是有效,但我通过重新启动Visual Studio解决了这个问题,重新打开了应用程序,选择了Build - &gt;清洁解决方案,重建然后运行。