简体   繁体   English

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

[英]System.Security.Cryptography.CryptographicException: The parameter is incorrect

I have the following exception when a try create a new Instance of ServiceHost (new ServiceHost(serviceType, baseAddresses)) 当尝试创建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

Here is the code that I use: 这是我使用的代码:

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);
    }

This is my Web.Config : 这是我的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 Config File, with all information about the Certificates: 证书配置文件,以及有关证书的所有信息:

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

Can you help? 你能帮我吗?

Based on the error message, the certificate referenced in the config file is incorrect. 根据错误消息,配置文件中引用的证书不正确。

Here is a good set of instructions from Microsoft on how to configure and use certificates. 这是 Microsoft提供的有关如何配置和使用证书的一组很好的说明

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.Security.Cryptography.CryptographicException:“参数不正确”/ Aes 256 Gcm 解密 - System.Security.Cryptography.CryptographicException: "The parameter is incorrect" / Aes 256 Gcm Decryption TwilioRequestValidator 中的瞬态 System.Security.Cryptography.CryptographicException - Transient System.Security.Cryptography.CryptographicException in TwilioRequestValidator System.Security.Cryptography.CryptographicException:句柄无效 - System.Security.Cryptography.CryptographicException: The handle is invalid System.Security.Cryptography.CryptographicException:'Cryptography_OAEPDecoding' - System.Security.Cryptography.CryptographicException: 'Cryptography_OAEPDecoding' System.Security.Cryptography.CryptographicException:系统找不到指定的文件 - System.Security.Cryptography.CryptographicException: The system cannot find the file specified 重置Microsoft Identity上的密码会导致System.Security.Cryptography.CryptographicException - Resetting password on Microsoft Identity causes System.Security.Cryptography.CryptographicException System.Security.Cryptography.CryptographicException:密钥集不存在 - System.Security.Cryptography.CryptographicException: keyset does not exist PrivateKey抛出了System.Security.Cryptography.CryptographicException类型的异常 - PrivateKey threw an exception of type System.Security.Cryptography.CryptographicException System.Security.Cryptography.CryptographicException:'输入数据不是一个完整的块。' - System.Security.Cryptography.CryptographicException: 'The input data is not a complete block.' System.Security.Cryptography.CryptographicException:RSACryptoserviceProvider中的长度错误 - System.Security.Cryptography.CryptographicException : Bad length in RSACryptoserviceProvider
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM