Table of contents

Introduction and goal

In this article, we will discuss how we can enable certificates on a WCF service. WCF has two modes by which it transfers data: transport and message.  This tutorial will concentrate on how we can enable certificates on the message mode of data transfer.

Nowadays I am distributing my 400 questions and answers ebook which covers major .NET related topics like WCF, WPF, WWF, AJAX, Core .NET, SQL Server, architecture,  and a lot more. I am sure you will enjoy this ebook: http://www.questpond.com/SampleDotNetInterviewQuestionBook.zip.  I have also been recording videos on .NET technologies, you can catch all the action here.

Beginner WCF FAQs

In case you are fresh to WCF, please refer the below two WCF FAQ articles:

  • WCF FAQ Part 1: This is a 20 question FAQ for beginners which explains the basic concepts of WCF like End Points,  contracts, and bindings. It also discusses the various hosting methodologies of WCF services. The article finally talks about bindings and one way operations in WCF.
  • WCF FAQ Part 2: This FAQ covers 10 questions which talks about concepts like duplex contracts,  hosting WCF on different protocols, MSMQ bindings, transaction isolation levels, and two way communication. The article also talks about two queues: volatile and dead letter queue.

Step 1: Create client and server certificates

Create two certificates, one for the server and the other for the client, using makecert.exe. You can get makecert.exe from  the “C:\Program Files\Microsoft Visual Studio 8\Common7\Tools\Bin” folder. You can go to the DOS prompt and run the below command snippet:

Hide    Copy Code
makecert.exe -sr CurrentUser -ss My -a sha1 -n CN=WCfServer -sky exchange -pe
makecert.exe -sr CurrentUser -ss My -a sha1 -n CN=WcfClient -sky exchange -pe

Below is a detailed explanation of the various attributes specified in makecert.exe.

Attribute Explanation

-sr

Specifies the Registry location of the certificate store. The SubjectCertStoreLocation argument must be either of the following:

  • currentUser: Specifies the registry location HKEY_CURRENT_USER.
  • localMachine: Specifies the registry location HKEY_LOCAL_MACHINE.

-ss

Specifies the name of the certificate store where the generated certificate is saved.

-a

Specifies the algorithm. Can be either MD5 or SHA1.

-n

Specifies a name for the certificate. This name must conform to the X.500 standard. The simplest method is to use  the "CN=MyName" format. If the /n switch is not specified, the default name of the certificate is "Joe's Software Emporium".

-sky

Specifies the key type. Can be either exchange or signature.

-pe

This makes the key exportable.

Note: Makecert.exe is a free tool provided by Microsoft which helps to create X.509 certificates that are signed by a system test root key or by another  specified key. This is a test certificate and not a real one and should not be used for production purposes. For production, buy proper certificates from Thawte, Verisign, GeoTrust, etc.

Currently, we have specified that we want to create the client key with the WcfClient name and server key with WCFServer. The certificates should be created  for the current user and should be exportable.

Once you run the command, you should see the Succeeded message as shown in the below figure. The below figure shows keys created for both the server and client.

Step 2: Copy the certificates in trusted people certificates

Go to Start -> Run and type MMC and press Enter. You will be popped with the MMC console. Click on File -> Add/remove snap-in.  You will be popped up with an Add/Remove snap-in, click on the Add button, select Certificates, and select ‘My user account’.

You can see the certificates created for the client and server in the personal certificates folder. We need to copy those certificates in the Trusted people -> Certificates folder.

Step 3: Specify the certification path and mode in the WCF service web.config file

Now that we have created both the certificates, we need to refer these certificates in our WCF project.  We have created two projects: one that has the WCF service and the other a web application which will consume the WCF service.

Let’s open the web.config file of the WCF service and enter two important things:

  • Where the certificate is stored, location, and how the WCF application should find it. This is defined using the serviceCertificate tag as shown in the below snippet.
  • certificationvalidationmode defines how the client certificates will be authenticated.
Certification validation mode Description

Chain trust

In this situation, the client certificate is validated against the root certificate.

Peer trust

PeerTrust ensures that the public key portion of the certificate is in the Trusted People certificate folder on the client's computer

ChainORPeertrust

This is just an OR condition for both chain and peer.

The above two points are clubbed together and entered in the web.config file of the WCF service.

Hide    Copy Code
<serviceCredentials>
<clientCertificate>
<authentication certificateValidationMode="PeerTrust"/>
</clientCertificate>
<serviceCertificate findValue="WCfServer"
storeLocation="CurrentUser"
storeName="My"
x509FindType="FindBySubjectName" />
</serviceCredentials>

Step 4: Define bindings

Now that we have defined our certificates and authentication type, we need to define that the authentication values will be sent through a message using certificates.  You can see we have defined the WsHttpBinding with a message attribute specifying that the WCF client needs to send a certificate for validation.

Hide    Copy Code
<bindings>
<wsHttpBinding>
<binding name="wsHttpEndpointBinding">
<security>
<message clientCredentialType="Certificate" />
</security>
</binding>
</wsHttpBinding>
</bindings>

Step 5: Tie up the bindings with the endpoint

Once done, we need to tie up this binding with the end point. This is done by using the bindingConfiguration tag as shown in the below code snippet.

Hide    Copy Code
<endpoint address="" binding="wsHttpBinding"
bindingConfiguration="wsHttpEndpointBinding" contract="WCFServiceCertificate.IService1">

Step 6: Make your web application client for consuming the WCF service

That’s all we need from the WCF service perspective. Compile the WCF service and reference it in the ASP.NET web application using ‘Service reference’.  Below is the code snippet where we have referenced the service and called the GetData function of the service.

Hide    Copy Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebConsumer.ServiceReference1;
namespace WebConsumer
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Service1Client obj = new Service1Client();
Response.Write(obj.GetData(12));
}
}
}

Now if you try to run the client, i.e., the web application, as it is, you should get an error as shown below. The error clearly indicates you can not use the WCF service  until you provide the client certificate.

Step 7: Define the certificates in the WCF client

Let's start the process of defining certificates in the WCF client. The way we have defined the authentication certification mode and the path of the certificate,  the same way we need to define it for the WCF client. You can see we have defined the authentication mode as peertrust and we have specified the client certificate  name as WcfClient.

Hide    Copy Code
<behaviors>
<endpointBehaviors>
<behavior name="CustomBehavior">
<clientCredentials>
<clientCertificate findValue="WcfClient" x509FindType="FindBySubjectName"
storeLocation="CurrentUser" storeName="My" />
<serviceCertificate>
<authentication certificateValidationMode="PeerTrust"/>
</serviceCertificate>
</clientCredentials>
</behavior>
</endpointBehaviors>
</behaviors>

Step 8: Tie up the behavior with the end point on the WCF client

We need to tie up the above defined behavior with the end point. You can see we have bound the behavior using the behaviorConfiguration property.  We also need to specify that the DNS value will be WcfServer which is your server certificate name.

Hide    Copy Code
<client>
<endpoint address="http://localhost:1387/Service1.svc" binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_IService1" contract="ServiceReference1.IService1"
name="WSHttpBinding_IService1" behaviorConfiguration="CustomBehavior">
<identity>
<dns value="WcfServer" />
</identity>
</endpoint>
</client>

Step 9: Enjoy your hard work

Once we are done, you can run the ASP.NET web app and you should see the below display.

Download code

You can download both the server and client code from here.

Nine simple steps to enable X.509 certificates on WCF- 摘自网络的更多相关文章

  1. [转]Getting started with ASP.NET Web API OData in 3 simple steps

    本文转自:https://blogs.msdn.microsoft.com/webdev/2013/01/29/getting-started-with-asp-net-web-api-odata-i ...

  2. [转载]Parsing X.509 Certificates with OpenSSL and C

    Parsing X.509 Certificates with OpenSSL and C Zakir Durumeric | October 13, 2013 While OpenSSL has b ...

  3. [No0000126]SSL/TLS原理详解与WCF中的WS-Security

    SSL/TLS作为一种互联网安全加密技术 1. SSL/TLS概览 1.1 整体结构 SSL是一个介于HTTP协议与TCP之间的一个可选层,其位置大致如下: SSL:(Secure Socket La ...

  4. Asp.net MVC十问十答[译]

    1. Explain MVC (Model-View-Controller) in general? MVC (Model-View-Controller) is an architectural s ...

  5. Change SSH Welcome Banner on Ubuntu

    One of the easiest way to protect and secure SSH logins by displaying warming message to UN-authoriz ...

  6. WCF学习系列四--【WCF Interview Questions – Part 4 翻译系列】

    WCF Interview Questions – Part 4   This WCF service tutorial is part-4 in series of WCF Interview Qu ...

  7. [转]How to enable macros in Excel 2016, 2013, and 2010

    本文转自:https://www.ablebits.com/office-addins-blog/2014/07/22/enable-macros-excel/#always-run-macros T ...

  8. 转载:10 Easy Steps to a Complete Understanding of SQL

    10 Easy Steps to a Complete Understanding of SQL 原文地址:http://tech.pro/tutorial/1555/10-easy-steps-to ...

  9. Simple Validation in WPF

    A very simple example of displaying validation error next to controls in WPF Introduction This is a ...

随机推荐

  1. XSS 攻击在它的面前都弱爆了!

    虽然双十一刚刚过去不久,但是对很多工程师来说,连续熬夜加班的「噩梦」似乎还没有过去.尤其是像双十一这种活动,对于电商网站的工程师们来说,他们需要彻夜的加班加点来保障网站的稳定性和安全性.当然,面对上千 ...

  2. ue标签不见了,如何解决?

    小问题,但是很恶心...如下图: 解决方法: 右键点击[菜单栏]右边的空白处,选择advanced,默认是basic,这时菜单栏中的菜单条目会变多,然后选择[视图]---[视图/列表]---[打开文件 ...

  3. linux 深入检测io详情的工具iopp

    1.为什么推荐iopp iotop对内核及python版本都有一定要求,有时候无法用上,这时候就可以使用iopp作为替代方案.在有些情况下可能无法顺利使用iotop,这时候就可以选择iopp了.它的作 ...

  4. JS插件excanvas的使用方法

     这个还没有想好怎么写,等写好后再发布 试用了excanvas.js,生成静态统计图 IE下使用excanvas.js的注意事项

  5. 用QT创建新风格: QStyle

    转贴: http://hi.baidu.com/yjj2008/blog/item/6cd4a1892ef0d4b60f2444a5.html 本文介绍了如何使用qt提供的接口来设计自己的GUI风格( ...

  6. 对C#中的web访问mysql数据库的一些知识点进行了整理归纳总结

    基本对比 使用方式 使用场合 优缺点 是否需要安装 需要的dll网址 引用方式 程序内引用 程序初期确定使用MySql,前期添加引用 大多数情况下使用在类文件内,多数使用于aspx,ashx等带有后置 ...

  7. SOA基础

    每个系统上都有多种业务逻辑,这就像在一个小超市中,一个人负责收银.清洁.摆货.咨询等各种各样的事情,当来超市的顾客多到一定程度,这个人就没办法再负责这么多的事情了,系统也同样如此. 第一个现象就是系统 ...

  8. 【HDOJ】4601 Letter Tree

    挺有意思的一道题,思路肯定是将图转化为Trie树,这样可以求得字典序.然后,按照trie的层次求解.一直wa的原因在于将树转化为线性数据结构时要从原树遍历,从trie遍历就会wa.不同结点可能映射为t ...

  9. git设置忽略某些文件或文件夹

    在git中如果想忽略掉某个文件,不让这个文件提交到版本库中,可以使用修改 .gitignore 文件的方法.如果没有 .gitignore 文件,就自己创建一个,手动创建会提示你输入文件名称,因此,你 ...

  10. Flash挡住DIV的解决方法

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://down ...