简体   繁体   English

在IIS中使用wsHttpBinding承载WCF服务的问题

[英]Problem in Hosting WCF Service using wsHttpBinding in IIS

I am trying to host my service using following configuration. 我正在尝试使用以下配置托管我的服务。

<system.serviceModel>
    <services>
        <service name="Test.MyService" behaviorConfiguration="MyServiceBehavior">
            <!--         Service Endpoints -->
            <endpoint address="MyTestService" binding="wsHttpBinding" bindingConfiguration="WebserviceHttpBinding" contract="Test.IMyService"/>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        </service>
    </services>
    <bindings>
        <wsHttpBinding>
            <binding name="WebserviceHttpBinding">
                <security mode="Message">
                    <message clientCredentialType="UserName" negotiateServiceCredential="false"/>
                </security>
            </binding>
        </wsHttpBinding>
    </bindings>
    <behaviors>
        <serviceBehaviors>
            <behavior name="MyServiceBehavior">
                <serviceCredentials>
                    <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="Test.CredentialValidator, Test"/>
                    <serviceCertificate findValue="RPKey" x509FindType="FindBySubjectName" storeLocation="LocalMachine" storeName="My"/>
                </serviceCredentials>
                <!--           To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
                <serviceMetadata httpGetEnabled="true"/>
                <!--           To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
                <serviceDebug includeExceptionDetailInFaults="true"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>

When I debug this service, I am having no problem. 当我调试该服务时,我没有问题。 I hosted this service in IIS using Website. 我使用网站在IIS中托管了此服务。 When I browse this service from IIS I am getting following exception. 当我从IIS浏览此服务时,出现以下异常。

Server Error in '/MyTestService' Application. “ / MyTestService”应用程序中的服务器错误。

Keyset does not exist 键集不存在

Description: An unhandled exception occurred during the execution of the current web request. 说明:执行当前Web请求期间发生未处理的异常。 Please review the stack trace for more information about the error and where it originated in the code. 请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息。

Exception Details: System.Security.Cryptography.CryptographicException: Keyset does not exist 异常详细信息:System.Security.Cryptography.CryptographicException:密钥集不存在

Source Error: 源错误:

An unhandled exception was generated during the execution of the current web request. 当前Web请求的执行期间生成了未处理的异常。 Information regarding the origin and location of the exception can be identified using the exception stack trace below. 可以使用下面的异常堆栈跟踪来标识有关异常的来源和位置的信息。

Stack Trace: 堆栈跟踪:

[CryptographicException: Keyset does not exist ] System.Security.Cryptography.Utils.CreateProvHandle(CspParameters parameters, Boolean randomKeyContainer) +369 System.Security.Cryptography.Utils.GetKeyPairHelper(CspAlgorithmType keyType, CspParameters parameters, Boolean randomKeyContainer, Int32 dwKeySize, SafeProvHandle& safeProvHandle, SafeKeyHandle& safeKeyHandle) +151 System.Security.Cryptography.RSACryptoServiceProvider.GetKeyPair() +85 System.Security.Cryptography.RSACryptoServiceProvider..ctor(Int32 dwKeySize, CspParameters parameters, Boolean useDefaultKeySize) +280 System.Security.Cryptography.X509Certificates.X509Certificate2.get_PrivateKey() +468 System.ServiceModel.Security.SecurityUtils.EnsureCertificateCanDoKeyExchange(X509Certificate2 certificate) +85 [CryptographicException:密钥集不存在] System.Security.Cryptography.Utils.CreateProvHandle(CspParameters参数,布尔型RandomKeyContainer)+369 System.Security.Cryptography.Utils.GetKeyPairHelper(CspAlgorithmType keyType,CspParameters参数,布尔型randomKeyConv,Handle,Size和Int32Prodw ,SafeKeyHandle和safeKeyHandle)+151 System.Security.Cryptography.RSACryptoServiceProvider.GetKeyPair()+85 System.Security.Cryptography.RSACryptoServiceProvider..ctor(Int32 dwKeySize,CspParameters参数,布尔值useDefaultKeySize)+280 System.Security.Cryptography.X509Certificate.X509 .get_PrivateKey()+468 System.ServiceModel.Security.SecurityUtils.EnsureCertificateCanDoKeyExchange(X509Certificate2证书)+85

[ArgumentException: The certificate 'CN=RPKey' must have a private key that is capable of key exchange. [ArgumentException:证书'CN = RPKey'必须具有能够进行密钥交换的私钥。 The process must have access rights for the private key.] System.ServiceModel.Security.SecurityUtils.EnsureCertificateCanDoKeyExchange(X509Certificate2 certificate) +15832031 System.ServiceModel.Security.ServiceCredentialsSecurityTokenManager.CreateServerX509TokenProvider() +45 System.ServiceModel.Security.ServiceCredentialsSecurityTokenManager.CreateLocalSecurityTokenProvider(RecipientServiceModelSecurityTokenRequirement recipientRequirement) +73 System.ServiceModel.Security.ServiceCredentialsSecurityTokenManager.CreateSecurityTokenProvider(SecurityTokenRequirement requirement) +65 System.ServiceModel.Security.SessionRenewSecurityTokenManager.CreateSecurityTokenProvider(SecurityTokenRequirement requirement) +14 System.ServiceModel.Security.SymmetricSecurityProtocolFactory.OnOpen(TimeSpan timeout) +15334232 System.ServiceModel.Security.WrapperSecurityCommunicationObject.OnOpen(TimeSpan timeout) +23 System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +563 System.Serv 该过程必须具有私钥的访问权限。] System.ServiceModel.Security.SecurityUtils.EnsureCertificateCanDoKeyExchange(X509Certificate2证书)+15832031 System.ServiceModel.Security.ServiceCredentialsSecurityTokenManager.CreateServerX509TokenProvider()+45 System.ServiceModel.Security.ServiceCredentialsSecurityTokenManager.CreateLocalSecurityTokenProvider( RecipientServiceModelSecurityTokenRequirement收件人要求)+73 System.ServiceModel.Security.ServiceCredentialsSecurityTokenManager.CreateSecurityTokenProvider(SecurityTokenRequirement要求)+65 System.ServiceModel.Security.SessionRenewSecurityTokenManager.CreateSecurityTokenProvider(SecurityTokenRequirement要求)+14 System.ServiceModel.Security.SymmetricSecurityProtocolFactory.OnOpen(TimeSpan超时)+14 System.ServiceModel.Security.WrapperSecurityCommunicationObject.OnOpen(TimeSpan超时)+23 System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan超时)+563 System.Serv iceModel.Security.SecurityListenerSettingsLifetimeManager.Open(TimeSpan timeout) +101 System.ServiceModel.Channels.SecurityChannelListener 1.OnOpen(TimeSpan timeout) +203 System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +563 System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpen(TimeSpan timeout) +87 System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +563 System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout) +110 System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +563 System.ServiceModel.Security.SecuritySessionSecurityTokenAuthenticator.OnOpen(TimeSpan timeout) +149 System.ServiceModel.Security.WrapperSecurityCommunicationObject.OnOpen(TimeSpan timeout) +23 System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +563 System.ServiceModel.Security.SecurityUtils.OpenCommunicationObject(ICommunicationObject obj, TimeSpan timeout) +24 System.ServiceModel.Security.SecuritySessionServerSettings.OnOpen(TimeSpan timeout) +878 System.ServiceModel.Security.WrapperSecurityCommunicationObject.OnOpen(TimeSpan timeout) +23 System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +563 System.ServiceModel.Security.SecurityListenerSettingsLifetimeManager.Open(TimeSpan timeout) +153 System.ServiceModel.Channels.SecurityChannelListener iceModel.Security.SecurityListenerSettingsLifetimeManager.Open(TimeSpan超时) 1.OnOpen(TimeSpan timeout) +203 System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +563 System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpen(TimeSpan timeout) +87 System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +563 System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout) +110 System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +563 System.ServiceModel.Security.SecuritySessionSecurityTokenAuthenticator.OnOpen(TimeSpan timeout) +149 System.ServiceModel.Security.WrapperSecurityCommunicationObject.OnOpen(TimeSpan timeout) +23 System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +563 System.ServiceModel.Security.SecurityUtils.OpenCommunicationObject(ICommunicationObject obj, TimeSpan timeout) +24 System.ServiceModel.Security.SecuritySessionServerSettings.OnOpen(TimeSpan timeout) +878 System.ServiceModel.Security.WrapperSecurityCommunicationObject.OnOpen(TimeSpan timeout) +23 System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +563 System.ServiceModel.Security.SecurityListenerSettingsLifetimeManager.Open(TimeSpan timeout) +153 System.ServiceModel.Channels.SecurityChannelListener System.ServiceModel.Channels.SecurityChannelListener 1.OnOpen(TimeSpan timeout) +203 System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +563 System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpen(TimeSpan timeout) +87 System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +563 System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout) +110 System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +563 System.ServiceModel.Security.SecuritySessionSecurityTokenAuthenticator.OnOpen(TimeSpan timeout) +149 System.ServiceModel.Security.WrapperSecurityCommunicationObject.OnOpen(TimeSpan timeout) +23 System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +563 System.ServiceModel.Security.SecurityUtils.OpenCommunicationObject(ICommunicationObject obj, TimeSpan timeout) +24 System.ServiceModel.Security.SecuritySessionServerSettings.OnOpen(TimeSpan timeout) +878 System.ServiceModel.Security.WrapperSecurityCommunicationObject.OnOpen(TimeSpan timeout) +23 System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +563 System.ServiceModel.Security.SecurityListenerSettingsLifetimeManager.Open(TimeSpan timeout) +153 System.ServiceModel.Channels.SecurityChannelListener 1.OnOpen(TimeSpan timeout) +203 System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +563 System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpen(TimeSpan timeout) +87 System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +563 System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout) +110 System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +563 System.ServiceModel.Security.SecuritySessionSecurityTokenAuthenticator.OnOpen(TimeSpan timeout) +149 System.ServiceModel.Security.WrapperSecurityCommunicationObject.OnOpen(TimeSpan timeout) +23 System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +563 System.ServiceModel.Security.SecurityUtils.OpenCommunicationObject(ICommunicationObject obj, TimeSpan timeout) +24 System.ServiceModel.Security.SecuritySessionServerSettings.OnOpen(TimeSpan timeout) +878 System.ServiceModel.Security.WrapperSecurityCommunicationObject.OnOpen(TimeSpan timeout) +23 System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +563 System.ServiceModel.Security.SecurityListenerSettingsLifetimeManager.Open(TimeSpan timeout) +153 System.ServiceModel.Channels.SecurityChannelListener 1.OnOpen(TimeSpan timeout) +203 System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +563 System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpen(TimeSpan timeout) +87 System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +563 System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout) +110 System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +563 System.ServiceModel.HostingManager.ActivateService(String normalizedVirtualPath) +135 System.ServiceModel.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath) +654 1.OnOpen(TimeSpan timeout) +203 System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +563 System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpen(TimeSpan timeout) +87 System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +563 System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout) +110 System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +563 System.ServiceModel.Security.SecuritySessionSecurityTokenAuthenticator.OnOpen(TimeSpan timeout) +149 System.ServiceModel.Security.WrapperSecurityCommunicationObject.OnOpen(TimeSpan timeout) +23 System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +563 System.ServiceModel.Security.SecurityUtils.OpenCommunicationObject(ICommunicationObject obj, TimeSpan timeout) +24 System.ServiceModel.Security.SecuritySessionServerSettings.OnOpen(TimeSpan timeout) +878 System.ServiceModel.Security.WrapperSecurityCommunicationObject.OnOpen(TimeSpan timeout) +23 System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +563 System.ServiceModel.Security.SecurityListenerSettingsLifetimeManager.Open(TimeSpan timeout) +153 System.ServiceModel.Channels.SecurityChannelListener 1.OnOpen(TimeSpan超时)+203 System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan超时)+563 System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpen(TimeSpan超时)+87 System.ServiceModel。 Channels.CommunicationObject.Open(TimeSpan超时)+563 System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan超时)+110 System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan超时)+563 System.ServiceModel.HostingManager.ActivateService(String normalizedVirtualPath) +135 System.ServiceModel.HostingManager.EnsureServiceAvailable(字符串normalizedVirtualPath)+654

[ServiceActivationException: The service '/AtlasServices/Service.svc' cannot be activated due to an exception during compilation. [ServiceActivationException:由于编译期间发生异常,因此无法激活服务'/AtlasServices/Service.svc'。 The exception message is: The certificate 'CN=RPKey' must have a private key that is capable of key exchange. 异常消息是:证书“ CN = RPKey”必须具有能够进行密钥交换的私钥。 The process must have access rights for the private key..] System.ServiceModel.AsyncResult.End(IAsyncResult result) +15700960 System.ServiceModel.Activation.HostedHttpRequestAsyncResult.End(IAsyncResult result) +15623609 System.ServiceModel.Activation.HostedHttpRequestAsyncResult.ExecuteSynchronous(HttpApplication context, Boolean flowContext) +265 System.ServiceModel.Activation.HttpModule.ProcessRequest(Object sender, EventArgs e) +227 System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +80 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +171 该进程必须具有私钥的访问权限。 (HttpApplication上下文,布尔型FlowContext)+265 System.ServiceModel.Activation.HttpModule.ProcessRequest(对象发送方,EventArgs e)+227 System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+80 System.Web。 HttpApplication.ExecuteStep(IExecutionStep步骤,布尔值并已完成同步)+171

This is propabely because on the IIS the account settings are different from the account that you are using to run a local server. 这可能是因为IIS上的帐户设置与您用于运行本地服务器的帐户不同。

are you working with an X.509 certificate? 您正在使用X.509证书吗? If so, are you sure you gave read acces privileges to the account that is running the process (that is running IIS) for the file containing the private key? 如果是这样,您确定对包含私钥的文件的运行进程(正在运行IIS)的帐户授予了读取acces特权吗?

So if IIS is running under the account Saghar, does Saghar have read privilesges for the key file ? 因此,如果IIS在Saghar帐户下运行,Saghar是否已读取密钥文件的特权?

@update @Update

ArgumentException: The certificate 'CN=RPKey' must have a private key that is capable of key exchange. ArgumentException:证书“ CN = RPKey”必须具有能够进行密钥交换的私钥。 The process must have access rights for the private key. 该过程必须具有私钥的访问权限。

this tells me that your IIS account doesn't have permissions to your private key 这告诉我您的IIS帐户无权访问您的私钥

Based on the exception report, it sounds like one of two things may be happening. 根据异常报告,听起来可能正在发生以下两种情况之一。 The certificate you are referencing was either installed into the keystore with only the public key, or the installed certificate is restricted and the account your application pool is running under does not have permission to access the key. 您引用的证书仅使用公共密钥安装到了密钥库中,或者安装的证书受到限制,并且您的应用程序池所运行的帐户无权访问该密钥。

The former case is very easy to run into if the x.509 certificate was not exported as a .pfx, but instead as a .cer. 如果未将x.509证书导出为.pfx,而是导出为.cer,则前一种情况非常容易遇到。 To exchange certificates that contain a private key, the .cer format is insufficient, as it can only contain a DER encoded x.509 public key certificate. 要交换包含私钥的证书,.cer格式是不够的,因为它只能包含DER编码的x.509公钥证书。 You must export your certificate from a certificate server (or from a store that has both keys) as a .pfx file, and make sure you include the private key. 您必须将证书从证书服务器(或同时具有两个密钥的商店)导出为.pfx文件,并确保包含私钥。

If it is the latter case, then you need to make sure the account of the application pool your service is running under in IIS has permission to access the certificate store the keys are contained within. 如果是后一种情况,则需要确保您的服务在IIS中运行的应用程序池帐户有权访问包含在其中的证书存储。 This article may be helpful: Make X.509 Certificates Accessible to WCF 本文可能会有所帮助: 使X.509证书可访问WCF

IIS application pool identity does not have access to certificate IIS应用程序池标识无权访问证书

  1. Set application pool identity to NETWORK SERVICE 将应用程序池标识设置为NETWORK SERVICE
  2. install rktools 安装rktools
  3. run "c:\\Program Files (x86)\\Windows Resource Kits\\Tools\\winhttpcertcfg.exe" -g -c LOCAL_MACHINE\\My -s WSE2QuickStartServer -a "NETWORK SERVICE" 运行“ c:\\ Program Files(x86)\\ Windows Resource Kits \\ Tools \\ winhttpcertcfg.exe” -g -c LOCAL_MACHINE \\ My -s WSE2QuickStartServer -a“网络服务”

If you do not require wshttpbinding, just try changing to basichttpbinding and your issue will probably go away. 如果您不需要wshttpbinding,只需尝试更改为basichttpbinding,您的问题可能会消失。

We had trouble with this in the past when the client was connecting from a site outside our network. 过去,当客户端从我们网络外部的站点进行连接时,我们遇到了麻烦。 Since the connection was over vpn we had the option of abandoning security for the binding. 由于连接是通过VPN进行的,因此我们可以选择放弃绑定安全性。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM