Working with Certificates - WCF (2024)

  • Article

To program Windows Communication Foundation (WCF) security, X.509 digital certificates are commonly used to authenticate clients and servers, encrypt, and digitally sign messages. This topic briefly explains X.509 digital certificate features and how to use them in WCF, and includes links to topics that explain these concepts further or that show how to accomplish common tasks using WCF and certificates.

In brief, a digital certificate is a part of a public key infrastructure (PKI), which is a system of digital certificates, certificate authorities, and other registration authorities that verify and authenticate the validity of each party involved in an electronic transaction through the use of public key cryptography. A certification authority issues certificates and each certificate has a set of fields that contain data, such as subject (the entity to which the certificate is issued), validity dates (when the certificate is valid), issuer (the entity that issued the certificate), and a public key. In WCF, each of these properties is processed as a Claim, and each claim is further divided into two types: identity and right. For more information about X.509 certificates see X.509 Public Key Certificates. For more information about Claims and Authorization in WCF see Managing Claims and Authorization with the Identity Model. For more information about implementing a PKI, see Enterprise PKI with Windows Server 2012 R2 Active Directory Certificate Services.

The primary function of a certificate is to authenticate the identity of the owner of the certificate to others. A certificate contains the public key of the owner, while the owner retains the private key. The public key can be used to encrypt messages sent to the owner of the certificate. Only the owner has access to the private key, so only the owner can decrypt those messages.

Certificates must be issued by a certification authority, which is often a third-party issuer of certificates. On a Windows domain, a certification authority is included that can be used to issue certificates to computers on the domain.

View certificates

To work with certificates, it is often necessary to view them and examine their properties. This is easily done with the Microsoft Management Console (MMC) snap-in tool. For more information, see How to: View Certificates with the MMC Snap-in.

Certificate stores

Certificates are found in stores. Two major store locations exist that are further divided into sub-stores. If you are the administrator on a computer, you can view both major stores by using the MMC snap-in tool. Non-administrators can view only the current user store.

  • The local machine store. This contains the certificates accessed by machine processes, such as ASP.NET. Use this location to store certificates that authenticate the server to clients.

  • The current user store. Interactive applications typically place certificates here for the computer's current user. If you are creating a client application, this is where you typically place certificates that authenticate a user to a service.

These two stores are further divided into sub-stores. The most important of these when programming with WCF include:

For more information about certificate stores, see Certificate Stores.

Select a store

Selecting where to store a certificate depends how and when the service or client runs. The following general rules apply:

  • If the WCF service is hosted in a Windows service use the local machine store. Note that administrator privileges are required to install certificates into the local machine store.

  • If the service or client is an application that runs under a user account, then use the current user store.

Access stores

Stores are protected by access control lists (ACLs), just like folders on a computer. When creating a service hosted by Internet Information Services (IIS), the ASP.NET process runs under the ASP.NET account. That account must have access to the store that contains the certificates a service uses. Each of the major stores is protected with a default access list, but the lists can be modified. If you create a separate role to access a store, you must grant that role access permission. To learn how to modify the access list using the WinHttpCertConfig.exe tool, see How to: Create Temporary Certificates for Use During Development.

Chain trust and certificate authorities

Certificates are created in a hierarchy where each individual certificate is linked to the CA that issued the certificate. This link is to the CA’s certificate. The CA’s certificate then links to the CA that issued the original CA’s certificate. This process is repeated up until the Root CA’s certificate is reached. The Root CA’s certificate is inherently trusted.

Digital certificates are used to authenticate an entity by relying on this hierarchy, also called a chain of trust. You can view any certificate's chain using the MMC snap-in by double-clicking any certificate, then clicking the Certificate Path tab. For more information about importing certificate chains for a Certification authority, see How to: Specify the Certificate Authority Certificate Chain Used to Verify Signatures.

Note

Any issuer can be designated a trusted root authority by placing the issuer's certificate in the trusted root authority certificate store.

Disable chain trust

When creating a new service, you may be using a certificate that is not issued by a trusted root certificate, or the issuing certificate itself may not be in the Trusted Root Certification Authorities store. For development purposes only, you can temporarily disable the mechanism that checks the chain of trust for a certificate. To do this, set the CertificateValidationMode property to either PeerTrust or PeerOrChainTrust. Either mode specifies that the certificate can either be self-issued (peer trust) or part of a chain of trust. You can set the property on any of the following classes.

ClassProperty
X509ClientCertificateAuthenticationX509ClientCertificateAuthentication.CertificateValidationMode
X509PeerCertificateAuthenticationX509PeerCertificateAuthentication.CertificateValidationMode
X509ServiceCertificateAuthenticationX509ServiceCertificateAuthentication.CertificateValidationMode
IssuedTokenServiceCredentialIssuedTokenServiceCredential.CertificateValidationMode

You can also set the property using configuration. The following elements are used to specify the validation mode:

  • <authentication>

  • <peerAuthentication>

  • <messageSenderAuthentication>

Custom authentication

The CertificateValidationMode property also enables you to customize how certificates are authenticated. By default, the level is set to ChainTrust. To use the Custom value, you must also set the CustomCertificateValidatorType attribute to an assembly and type used to validate the certificate. To create a custom validator, you must inherit from the abstract X509CertificateValidator class.

When creating a custom authenticator, the most important method to override is the Validate method. For an example of custom authentication, see the X.509 Certificate Validator sample. For more information, see Custom Credential and Credential Validation.

Use the PowerShell New-SelfSignedCertificate cmdlet to build a certificate chain

The PowerShell New-SelfSignedCertificate cmdlet creates X.509 certificates and private key/public key pairs. You can save the private key to disk and then use it to issue and sign new certificates, thus simulating a hierarchy of chained certificates. The cmdlet is intended for use only as an aid when developing services and should never be used to create certificates for actual deployment. When developing a WCF service, use the following steps to build a chain of trust with the New-SelfSignedCertificate cmdlet.

  1. Create a temporary root authority (self-signed) certificate using the New-SelfSignedCertificate cmdlet. Save the private key to the disk.

  2. Use the new certificate to issue another certificate that contains the public key.

  3. Import the root authority certificate into the Trusted Root Certification Authorities store.

  4. For step-by-step instructions, see How to: Create Temporary Certificates for Use During Development.

Which certificate to use?

Common questions about certificates are which certificate to use, and why. The answer depends on whether you are programming a client or service. The following information provides a general guideline and is not an exhaustive answer to these questions.

Service certificates

Service certificates have the primary task of authenticating the server to clients. One of the initial checks when a client authenticates a server is to compare the value of the Subject field to the Uniform Resource Identifier (URI) used to contact the service: the DNS of both must match. For example, if the URI of the service is http://www.contoso.com/endpoint/ then the Subject field must also contain the value www.contoso.com.

Note that the field can contain several values, each prefixed with an initialization to indicate the value. Most commonly, the initialization is "CN" for common name, for example, CN = www.contoso.com. It is also possible for the Subject field to be blank, in which case the Subject Alternative Name field can contain the DNS Name value.

Also note the value of the Intended Purposes field of the certificate should include an appropriate value, such as "Server Authentication" or "Client Authentication".

Client certificates

Client certificates are not typically issued by a third-party certification authority. Instead, the Personal store of the current user location typically contains certificates placed there by a root authority, with an intended purpose of "Client Authentication". The client can use such a certificate when mutual authentication is required.

Online revocation and offline revocation

Certificate validity

Every certificate is valid only for a given period of time, called the validity period. The validity period is defined by the Valid from and Valid to fields of an X.509 certificate. During authentication, the certificate is checked to determine whether the certificate is still within the validity period.

Certificate revocation list

At any time during the validity period, the certification authority can revoke a certificate. This can occur for many reasons, such as a compromise of the private key of the certificate.

When this occurs, any chains that descend from the revoked certificate are also invalid, and are not trusted during authentication procedures. To find out which certificates are revoked, each issuer publishes a time- and date-stamped certificate revocation list (CRL). The list can be checked using either online revocation or offline revocation by setting the RevocationMode or DefaultRevocationMode property of the following classes to one of the X509RevocationMode enumeration values: X509ClientCertificateAuthentication, X509PeerCertificateAuthentication, X509ServiceCertificateAuthentication, and the IssuedTokenServiceCredential classes. The default value for all properties is Online.

You can also set the mode in configuration using the revocationMode attribute of both the <authentication> (of the <serviceBehaviors>) and the <authentication> (of the <endpointBehaviors>).

The SetCertificate method

In WCF, you must often specify a certificate or set of certificates a service or client is to use to authenticate, encrypt, or digitally sign a message. You can do this programmatically by using the SetCertificate method of various classes that represent X.509 certificates. The following classes use the SetCertificate method to specify a certificate.

ClassMethod
PeerCredentialSetCertificate
X509CertificateInitiatorClientCredentialSetCertificate
X509CertificateRecipientServiceCredentialSetCertificate
X509CertificateInitiatorServiceCredential
SetCertificate

The SetCertificate method works by designating a store location and store, a "find" type (x509FindType parameter) that specifies a field of the certificate, and a value to find in the field. For example, the following code creates a ServiceHost instance and sets the service certificate used to authenticate the service to clients with the SetCertificate method.

Uri baseAddress = new Uri("http://cohowinery.com/services");ServiceHost sh = new ServiceHost(typeof(CalculatorService), baseAddress );sh.Credentials.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My,X509FindType.FindBySubjectName, "cohowinery.com");
Dim baseAddress As New Uri("http://cohowinery.com/services")Dim sh As New ServiceHost(GetType(CalculatorService), baseAddress)sh.Credentials.ServiceCertificate.SetCertificate( _StoreLocation.LocalMachine, StoreName.My, _X509FindType.FindBySubjectName, "cohowinery.com")

Multiple certificates with the same value

A store may contain multiple certificates with the same subject name. This means that if you specify that the x509FindType is FindBySubjectName or FindBySubjectDistinguishedName, and more than one certificate has the same value, an exception is thrown because there is no way to distinguish which certificate is required. You can mitigate this by setting the x509FindType to FindByThumbprint. The thumbprint field contains a unique value that can be used to find a specific certificate in a store. However, this has its own disadvantage: if the certificate is revoked or renewed, the SetCertificate method fails because the thumbprint is also gone. Or, if the certificate is no longer valid, authentication fails. The way to mitigate this is to set the x590FindType parameter to FindByIssuerName and specify the issuer's name. If no particular issuer is required, you can also set one of the other X509FindType enumeration values, such as FindByTimeValid.

Certificates in configuration

You can also set certificates by using configuration. If you are creating a service, credentials, including certificates, are specified under the <serviceBehaviors>. When you are programming a client, certificates are specified under the <endpointBehaviors>.

Map a certificate to a user account

A feature of IIS and Active Directory is the ability to map a certificate to a Windows user account. For more information about the feature, see Map certificates to user accounts.

For more information about using Active Directory mapping, see Mapping Client Certificates with Directory Service Mapping.

With this capability enabled, you can set the MapClientCertificateToWindowsAccount property of the X509ClientCertificateAuthentication class to true. In configuration, you can set the mapClientCertificateToWindowsAccount attribute of the <authentication> element to true, as shown in the following code.

<serviceBehaviors> <behavior name="MappingBehavior"> <serviceCredentials> <clientCertificate> <authentication certificateValidationMode="None" mapClientCertificateToWindowsAccount="true" /> </clientCertificate> </serviceCredentials> </behavior></serviceBehaviors>

Mapping an X.509 certificate to the token that represents a Windows user account is considered an elevation of privilege because, once mapped, the Windows token can be used to gain access to protected resources. Therefore, domain policy requires the X.509 certificate to comply with its policy prior to mapping. The SChannel security package enforces this requirement.

When using .NET Framework 3.5 or later versions, WCF ensures the certificate conforms to domain policy before it is mapped to a Windows account.

In the first release of WCF, mapping is done without consulting the domain policy. Therefore it is possible that older applications that used to work when running under the first release, fails if the mapping is enabled and the X.509 certificate does not satisfy the domain policy.

See also

  • System.ServiceModel.Channels
  • System.ServiceModel.Security
  • System.ServiceModel
  • X509FindType
  • Securing Services and Clients
Working with Certificates - WCF (2024)

FAQs

Is WCF obsolete? ›

Windows Communication Framework (WCF) may be deprecated in . NET 5/6+, but it doesn't mean your applications are going to be left out in the cold. Just like Web Forms and other . NET Framework technologies, your WCF applications will continue to work for a long time.

How to implement certificate-based authentication in WCF? ›

In a nutshell, you copy a . PFX file (cert and private key) to the clients and host and set them up in the cert store (Like LocalComputer\Trusted, for instance). Then you set up the client and host WCF software to a) access the certificates and b) make get the host to deny access to non-authenticated users.

What replaced WCF? ›

Microsoft generally recommends two alternatives, gRPC and Web API, to replace WCF.

What is the difference between WCF and gRPC? ›

gRPC is a modern RPC framework that is a popular alternative to WCF. gRPC is built on top of HTTP/2, which provides a number of advantages over WCF, including: Performance: gRPC is much more efficient than WCF, especially for long-running connections.

Is WCF worth learning? ›

Originally Answered: is it worth learning WCF ? It is worth learning for those who want to be a developer and learn to build services on a company network or as part of an enterprise system. So, in case, you want to be a developer, it will be beneficial for you.

What is better than WCF? ›

WEB API can use any text format including XML and is faster than WCF. WEB API can be used to create full-blown REST Services. WEB API doesn't require any data contracts and doesn't require configurations to the level of WCF.

How does certificate based authentication work? ›

Certificate-based authentication is the process of establishing your identity using electronic documents known as digital certificates. A digital certificate is like an electronic passport used to prove your identity by confirming your ownership of a private key.

How do I set up certificate based authentication? ›

Certificate-based authentication
  1. Load required key pairs and certificates for Code Signing.
  2. Prepare Circle of Trust certificates.
  3. Import and install certificates for Circle of Trust.
  4. Turn on Code Signing.
  5. Create Code Signing key pairs and certificates.
  6. Specify custom rules in ECC firewall.

How do I connect to Active Directory with certificates? ›

After a certificate is installed, follow these steps to verify that LDAPS is enabled:
  1. Start the Active Directory Administration Tool (Ldp.exe).
  2. On the Connection menu, click Connect.
  3. Type the name of the domain controller to which you want to connect.
  4. Type 636 as the port number.
  5. Click OK.
Feb 19, 2024

What might replace WCF in .NET Core? ›

You can use gRPC for hosting web services inside . NET core application. gRPC is a high performance, open source RPC framework initially developed by Google. The framework is based on a client-server model of remote procedure calls.

Is .NET remoting dead? ›

. NET Remoting was identified as a problematic architecture. It's used for cross-AppDomain communication, which is no longer supported.

Which is better WCF or Web API? ›

WCF is the ideal choice when you need to create a service that supports unique scenarios such as duplex communication, one way messaging and message queues, among others. Web API should be the option when you need to build resource-oriented services over HTTP that can utilize the full features of HTTP.

When to choose WCF over Web API? ›

WCF is used for SOAP-based service development, whereas Web API is utilized for both SOAP-based and RESTful service development. WCF does not provide support for MVC functionalities, although Web API does.

Can we replace WCF with Web API? ›

Create a “standard” ASP.NET Core Web API project for the new server, and add a reference to the new class library project. This is necessary to enable the server to use the same replacement classes (previously DataContract) as the client. Now create a controller for each previous WCF ServiceContract in the Web API.

Is it possible to use WCF as RESTful services? ›

You can implement REST and SOAP in WCF services. You can refer to the following code, the WCF service publishes two endpoints, one is a SOAP service and the other is a REST service.

Is Windows Workflow Foundation dead? ›

While it isn't dead, it certainly isn't thriving. WF is tied to . NET Framework, which in turn is tied to the Operating System. WF will last until its Framework reaches the end of its life; so it'll continue to receive security updates and occasional bug fixes, but it won't get any new features.

What is the alternative to WCF for .NET 6? ›

If you want to use . NET 6, I recommend you to use an alternative to WCF: gRPC,CoreWCF,ASP.NET Core MVC. Or use the WCF Web Services Reference Provider Tool, a Visual Studio Connected Services extension that lets you connect .

Does Web API replace WCF? ›

WCF Web API is replaced by ASP.NET Web API which takes features from WCF Web API and merges them with the features from ASPNet MVC.

Top Articles
Latest Posts
Article information

Author: Merrill Bechtelar CPA

Last Updated:

Views: 6268

Rating: 5 / 5 (70 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Merrill Bechtelar CPA

Birthday: 1996-05-19

Address: Apt. 114 873 White Lodge, Libbyfurt, CA 93006

Phone: +5983010455207

Job: Legacy Representative

Hobby: Blacksmithing, Urban exploration, Sudoku, Slacklining, Creative writing, Community, Letterboxing

Introduction: My name is Merrill Bechtelar CPA, I am a clean, agreeable, glorious, magnificent, witty, enchanting, comfortable person who loves writing and wants to share my knowledge and understanding with you.