Enable Https for Asp.Net WEB API 2 Self-Host

2019-07-13 07:04发布

It costs me 2 days already to make my web service support Https, the interesting thing is I could not find any official document on how to implement this common requirement.

by referring some posts, this is what I did:

  • Applied a Http certificate from authority, and import to Windows key store.

    Now I got 2 script commands like:

netsh http add urlacl url=http://+:9999/ user=Everyone

netsh http add sslcert ipport=0.0.0.0:9999 certhash=XXXXXXXXXXXXXXXXXXXX appid={12345678-db90-4b66-8b01-88f7af2e36bf}

  • modify the code:

    string baseAddress = "https://+:9999/";
    try
    {
        // Start OWIN host 
        using (WebApp.Start<SelfHostStartup>(url: baseAddress))
        {
             //.....
    
  • Run that 2 script commands first, looks fine. Run my application, error pop up:

    System.Reflection.TargetInvocationException:
    Exception has been thrown by the target of an invocation. ---> System.Net.HttpLi
    stenerException: Failed to listen on prefix 'https://+:9999/' because it conflic
    ts with an existing registration on the machine.
       at System.Net.HttpListener.AddAllPrefixes()
       at System.Net.HttpListener.Start()
       at Microsoft.Owin.Host.HttpListener.OwinHttpListener.Start(HttpListener liste
    ner, Func`2 appFunc, IList`1 addresses, IDictionary`2 capabilities, Func`2 logge
    rFactory)
       at Microsoft.Owin.Host.HttpListener.OwinServerFactory.Create(Func`2 app, IDic
    tionary`2 properties)
       --- End of inner exception stack trace ---
       at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments,
     Signature sig, Boolean constructor)
       at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Objec
    t[] parameters, Object[] arguments)
       at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invoke
    Attr, Binder binder, Object[] parameters, CultureInfo culture)
       at Microsoft.Owin.Hosting.ServerFactory.ServerFactoryAdapter.Create(IAppBuild
    er builder)
       at Microsoft.Owin.Hosting.Engine.HostingEngine.StartServer(StartContext conte
    xt)
       at Microsoft.Owin.Hosting.Engine.HostingEngine.Start(StartContext context)
       at Microsoft.Owin.Hosting.Starter.DirectHostingStarter.Start(StartOptions opt
    ions)
       at Microsoft.Owin.Hosting.Starter.HostingStarter.Start(StartOptions options)
       at Microsoft.Owin.Hosting.WebApp.StartImplementation(IServiceProvider service
    s, StartOptions options)
       at Microsoft.Owin.Hosting.WebApp.Start(StartOptions options)
       at Microsoft.Owin.Hosting.WebApp.Start[TStartup](StartOptions options)
       at Microsoft.Owin.Hosting.WebApp.Start[TStartup](String url)
       at WayneCloud.Program.Main(String[] args)
    

any idea?

[edit0]: by using netstat -an, I could not see port 9999 on list.

[edit1]: by using netsh http delete urlacl url=http://+:9999/, the exception was gone, now the 9999 was on listening in netstat -an, I tried using IE to access a https url, it give me a 500 error.

1条回答
你好瞎i
2楼-- · 2019-07-13 07:11

I was able to put it to work using WebServiceHost object to host my service.Here all the steps:

1) Start the service using System.ServiceModel.Web.WebServiceHost at Main entry point

  webServiceHost = new WebServiceHost(typeof(WF_HttpService));
  webServiceHost.Open();     

2) configure the service baseAddress and binding like this:

<webHttpBinding>
    <binding name="ssl">
      <security mode="Transport" /> 
    </binding>
  </webHttpBinding>


<service name="WF_WSRV.Services.WF_HttpService">
    <endpoint binding="webHttpBinding" name="WF" bindingConfiguration="ssl" contract="...IWF_HttpService" />
    <host>
      <baseAddresses>
        <add baseAddress="https://localhost:4443/WF" />
      </baseAddresses>
    </host>
  </service>

3) On an elevated console (“Run as administrator”), execute:

netsh http add urlacl url=https://+:4443/WF user=domain\john doe

netsh http add sslcert ipport=0.0.0.0:4443 certhash=576a6bdddfeeb499b6b41d5d70d818755b483fc0 appid="{00000000-0000-0000-0000-000000000000}" where certhash it the thumbnail of the certificate.

To avoid adding certificate installation errors (certificates have to be installed in the proper folder in the certificate store), i created an self-sign certificate in IIS 8 console. The IIS create and installs it in one step.

I am using .net 4.7.

查看更多
登录 后发表回答