我正在托管带有自定义证书验证的WCF Web服务,但我无法正确配置它。当我尝试获取WebService的WSDL时,我在下面得到一个编译错误。我做错了什么?
由于
我已查看过:Custom certificate validation in WCF service和authentication of clientCertificate Element以及How to: Create a Service that Employs a Custom Certificate Validator和X.509 Certificate Validator,这些链接都没有描述我遇到的问题。
Could not load file or assembly 'service' or one of its dependencies. The system cannot find the file specified.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.IO.FileNotFoundException: Could not load file or assembly 'service' or one of its dependencies. The system cannot find the file specified.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="TransportSecurity">
<security mode="Message">
<message clientCredentialType="Certificate" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceBehavior">
<serviceMetadata httpsGetEnabled="true" httpsGetUrl="" />
<serviceDebug includeExceptionDetailInFaults ="true"/>
<serviceCredentials>
<clientCertificate>
<authentication certificateValidationMode="Custom" customCertificateValidatorType = "MyProject.MyX509CertificateValidator, service"/>
</clientCertificate>
<serviceCertificate findValue="hashvalue" storeLocation="LocalMachine" storeName="My" x509FindType="FindByThumbprint" />
</serviceCredentials>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="clientBehavior">
<clientCredentials>
<serviceCertificate>
<authentication certificateValidationMode="Custom" customCertificateValidatorType="MyProject.MyX509CertificateValidator, client"/>
</serviceCertificate>
</clientCredentials>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="MyProject.MyProjectWCF" behaviorConfiguration="MyServiceBehavior">
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="TransportSecurity" contract="MyProject.IMyProjectWCF" />
<endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
Imports System.ServiceModel
Imports System.ServiceModel.Description
Imports System.IdentityModel.Selectors
Imports System.Security.Cryptography.X509Certificates
Imports System.IdentityModel.Tokens
Imports System.ServiceModel.Security
Namespace MyProject
' NOTE: You can use the "Rename" command on the context menu to change the class name "MyProjectWCF" in code, svc and config file together.
<ServiceBehavior()> _
Public Class MyProjectWCF
Implements IMyProjectWCF
Public Function HelloWorld() As String Implements IMyProjectWCF.HelloWorld
Return "nameSpace: [" + Me.GetType().Namespace + "]" + vbNewLine + "Normal response"
End Function
Sub New()
Dim serviceHost As New ServiceHost(GetType(MyProjectWCF))
Try
serviceHost.Credentials.ClientCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.Custom
serviceHost.Credentials.ClientCertificate.Authentication.CustomCertificateValidator = New MyX509CertificateValidator("CN=MyCertificate")
serviceHost.Open()
'serviceHost.Close()
Finally
'serviceHost.Close()
End Try
End Sub
End Class
Public Class MyX509CertificateValidator
Inherits X509CertificateValidator
Private allowedIssuerName As String
Public Sub New(ByVal allowedIssuerName As String)
If allowedIssuerName Is Nothing Then
Throw New ArgumentNullException("allowedIssuerName")
End If
Me.allowedIssuerName = allowedIssuerName
End Sub
Public Overrides Sub Validate(ByVal certificate As X509Certificate2)
' Check that there is a certificate.
If certificate Is Nothing Then
Throw New ArgumentNullException("certificate")
End If
' Check that the certificate issuer matches the configured issuer.
If allowedIssuerName <> certificate.IssuerName.Name Then
Throw New SecurityTokenValidationException _
("Certificate was not issued by a trusted issuer")
End If
End Sub
End Class
End Namespace
Imports System.ServiceModel
Imports System.Security.Permissions
Namespace MyProject
' NOTE: You can use the "Rename" command on the context menu to change the interface name "IMyProjectWCF" in both code and config file together.
<ServiceContract([Namespace]:="MyProject")> _
Public Interface IMyProjectWCF
<OperationContract()> _
Function HelloWorld() As String
End Interface
End Namespace
将默认构造函数插入cert验证器类:
Public Sub New()
Me.New("CN=yourCertificate here")
End Sub
然后我必须弄清楚我的网站的项目名称是什么,即App_Code,它将一堆其他页面编译成一个DLL,即APP_Code.dll。 web.config中的最后一行如下所示:
<authentication certificateValidationMode="Custom" customCertificateValidatorType="MyProject.MyX509CertificateValidator, App_Code"/>
所以现在没有编译错误,我得到了我的WSDL。谢谢你的帮助:))
答案 0 :(得分:2)
我认为你必须改变这个
customCertificateValidatorType = “MyProject.MyX509CertificateValidator,service”/&gt;
到
customCertificateValidatorType = “MyProject.MyX509CertificateValidator,MyProject”/&gt;
因为“服务”它不在您的命名空间中。也许你是从MSDN粘贴它,但你必须认为MSDN WCF演示项目('101个样本')曾经被称为'服务'。