Self Hosting a secured ASP.Net Web API

3 בMarch 2016

no comments

 

Recently I had to build a self-hosted service that needs also to be contacted via
SSL. I’ve found a few guides on the internet however they weren’t updated to
the Owin.SelfHost so I’ve decided to write one of my own.

 

So, Here it is.

1, On your service you need to have a startup class, the same as
you might have on your ASP.Net application.  There is nothing different
between a regular and secured service.

public class Startup
{
// This code configures Web API. The Startup class is specified as a type
// parameter in the WebApp.Start method.
public void Configuration(IAppBuilder appBuilder)
{
// Configure Web API for self-host.
HttpConfiguration config = new HttpConfiguration();
config.EnableCors();
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
appBuilder.UseWebApi(config);
appBuilder.UseFileServer(options: new Microsoft.Owin.StaticFiles.FileServerOptions
{
EnableDirectoryBrowsing = false,
EnableDefaultFiles = true
});
}
}

2. You need to install a certificate into your Machine Store/Personal. You may
use the IIS Express  self signed certificate for your development  server.

3. Go to your certificate’s personal store and extract the thumbprint (hash) of your
certificate. You may see it on the following screenshot

certificates

4. On command prompt, using administrative priviliges, run the following command
netsh http add sslcert ipport=0.0.0.0<port> certhash=<Your hash> appid={00000000-0000-0000-0000-AABBCCDDEEFF}

5. When starting the hot using  WebApp.Start, make sure you allocate the same port you assigned on stage 4.

6. Run your windows service / application.

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*