【问题标题】:System.Security.Cryptography.CryptographicException: The parameter is incorrectSystem.Security.Cryptography.CryptographicException:参数不正确
【发布时间】:2011-12-09 16:35:53
【问题描述】:

尝试创建新的 ServiceHost 实例时出现以下异常 (new 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>

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

<?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>

你能帮忙吗?

【问题讨论】:

    标签: c# wcf x509


    【解决方案1】:

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

    来自 Microsoft 的Here is a good set of instructions,了解如何配置和使用证书。

    【讨论】:

    • 第一次使用证书,不太了解web.config如何配置。这是我正在使用的文件配置:
    • @nunofamel:您可以编辑您的问题并在 web.config 中包含您的证书信息吗?
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-23
      • 2019-05-23
      • 2015-05-27
      • 2012-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-25
      相关资源
      最近更新 更多