Implement SSL in self-hosted Web Api

This article is to show you how to add SSL to Self-Hosted Web Api

Maybe some of the people don’t like IIS. IIS comes with all the Windows and handle many technical operation so as a developer, we can focus on the core competency of the business.

For those people who want to implement SSL in self-hosted Web Api that is what I found out after reading blogs and msdn.

1. Create a class file inherit from HttpSelfHostConfiguration.

internal class EExtendHttpSelfHostConfigurationpublic : HttpSelfHostConfiguration


    public EExtendHttpSelfHostConfigurationpublic(string baseAddress)

        : base(baseAddress)




    protected override BindingParameterCollection OnConfigureBinding(HttpBinding httpBinding)


        httpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;


        httpBinding.Security.Mode = HttpBindingSecurityMode.Transport;


        return base.OnConfigureBinding(httpBinding);



2. Put this code in main program.

private static void Main()


    string sslAddress =”;

    var config = new EExtendHttpSelfHostConfigurationpublic(sslAddress);

    //Need tp refactor to use the route in Startup.cs


        name: “DefaultApi”,

        routeTemplate: “api/{controller}/{id}”,

        defaults: new { id = RouteParameter.Optional }


    using (HttpSelfHostServer server = new HttpSelfHostServer(config))



        Console.WriteLine(“Press Enter to quit.”);




The sample way for you is download the sample from then modify it.

This is not end yet. You need to add the SSL certification, generate a certificate from IIS if you do not have one, using netsh command. Go to command prompt and type the below.

netsh http add urlacl url=https://+:4443/ user=Everyone

netsh http add sslcert ipport= certhash=5dc0ca6239d24e1419ec68502c92afef5756f886 appid={1EA348FB-F83E-4AC8-B909-372BBA9B08E9}

Do not follow exactly, you should have your certhash from your own certificate. You can also generate another guid from Visual Studio for appid.

You can now run the application.

You can also download a single solution from The sample file name is My MSN ID is

About chanmingman

Since March 2011 Microsoft Live Spaces migrated to Wordpress ( till now, I have is over 1 million viewers. This blog is about more than 50% telling you how to resolve error messages, especial for Microsoft products. The blog also has a lot of guidance teaching you how to get stated certain Microsoft technologies. The blog also uses as a help to keep my memory. The blog is never meant to give people consulting services or silver bullet solutions. It is a contribution to the community. Thanks for your support over the years. Ming Man is Microsoft MVP since year 2006. He is a software development manager for a multinational company. With 25 years of experience in the IT field, he has developed system using Clipper, COBOL, VB5, VB6, VB.NET, Java and C #. He has been using Visual Studio (.NET) since the Beta back in year 2000. He and the team have developed many projects using .NET platform such as SCM, and HR based applications. He is familiar with the N-Tier design of business application and is also an expert with database experience in MS SQL, Oracle and AS 400.
This entry was posted in .Net and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s