Search code examples
vb.netweb-serviceswcfwcf-securityx509certificate2

WCF webservice with custom certificate validation


I am hosting a WCF webservice with custom certificate validation, but I am not able to configure it properly. When I try to get the WSDL of the WebService, I get a compilation error below. What am I doing wrong?

Thanks

Edit:

I've looked into: Custom certificate validation in WCF service and authentication of clientCertificate Element and How to: Create a Service that Employs a Custom Certificate Validator and X.509 Certificate Validator and none of those links describe an issue I am having.

Compilation Error message:

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.

web.config:

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

WCF code:

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

Interface code:

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

EDIT 2 (with fix):

Insert default constructor into the cert validator class:

    Public Sub New()
        Me.New("CN=yourCertificate here")
    End Sub

And then I had to figure out what the project name of my website is, which is App_Code, it gets compiled with a bunch of other pages into one DLL, which is APP_Code.dll. The final line in web.config looks like this:

<authentication certificateValidationMode="Custom" customCertificateValidatorType="MyProject.MyX509CertificateValidator, App_Code"/>

So now there are no compiled errors and I get my WSDL. Thank you for your help :)


Solution

  • I think that you have to change this

    customCertificateValidatorType = "MyProject.MyX509CertificateValidator, service"/>

    to

    customCertificateValidatorType = "MyProject.MyX509CertificateValidator, MyProject"/>

    Because 'service' it's not in your namespace. Maybe you are pasting it from MSDN, but you have to think that the MSDN WCF demo projects ('101 samples'), used to be called 'service'.