为Asp.Net WEB API 2自主机启用Https

时间:2016-02-19 08:26:55

标签: asp.net asp.net-web-api https

我已经花了2天的时间来支持我的Web服务Https,有趣的是我找不到任何关于如何实现这个常见要求的官方文档。

通过引用一些帖子,这就是我所做的:

  • 从权限中应用Http证书,并导入到Windows密钥库。

    现在我有两个脚本命令,如:

  

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}

  • 修改代码:

    string baseAddress = "https://+:9999/";
    try
    {
        // Start OWIN host 
        using (WebApp.Start<SelfHostStartup>(url: baseAddress))
        {
             //.....
    
  • 首先运行这2个脚本命令,看起来很好。运行我的应用程序,弹出错误:

    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)
    

任何想法?

[edit0]: 使用netstat -an,我无法在列表中看到端口9999

[EDIT1]: 使用netsh http delete urlacl url=http://+:9999/,异常消失,现在9999正在netstat -an监听,我尝试使用IE访问https网址,它给我一个500错误

1 个答案:

答案 0 :(得分:0)

我能够使用WebServiceHost对象将其用于托管我的服务。以下是所有步骤:

1)在Main入口点使用System.ServiceModel.Web.WebServiceHost启动服务

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

2)配置服务baseAddress和绑定,如下所示:

<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)在提升的控制台上(“以管理员身份运行”),执行:

  

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 =&#34; {00000000-0000-0000-0000-000000000000}&#34;   certhash是证书的缩略图。

为避免添加证书安装错误(证书必须安装在证书存储区中的适当文件夹中),我在IIS 8控制台中创建了自签名证书。 IIS只需一步即可创建并安装它。

我正在使用.net 4.7。