【问题标题】:Enable Https for Asp.Net WEB API 2 Self-Host为 Asp.Net WEB API 2 自托管启用 Https
【发布时间】:2016-05-31 17:57:27
【问题描述】:

我花了 2 天时间才让我的 web 服务支持 Https,有趣的是我找不到任何关于如何实现这个通用要求的官方文档。

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

  • 应用了来自权威机构的 Http 证书,并导入到 Windows 密钥库。

    现在我得到了 2 个脚本命令,例如:

netsh http 添加 urlacl url=http://+:9999/user=Everyone

netsh http add sslcert ipport=0.0.0.0:9999 certash=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)
    

有什么想法吗?

[编辑0]: 通过使用netstat -an,我在列表中看不到端口9999

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

【问题讨论】:

  • 我发表评论只是因为我不确定您的问题的解决方案,但我会提供两件事:1. 不要在您的 baseAddress 中使用“+”。使用“本地主机”。 2. 尝试另一个端口。它说它有冲突,所以也许 something else 绑定在 9999 上?
  • @ChrisSimmons 感谢您的快速回复!刚试了local host,还是一样的错误。看到我的edit0netstat -an 甚至看不到9999 在列表中。
  • 您的 netsh 命令“netsh http add urlacl url=http://+:9999/ user=Everyone”绑定到 HTTP,而不是 HTTPS。将其更改为“netsh http add urlacl url=https://+:9999/ user=Everyone”。
  • 您的 API 是否可以访问?我在启动 WebAPI 时没有任何问题,但是我无法从客户端访问它,包括邮递员。
  • 没关系,我搞定了!

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


【解决方案1】:

我可以使用 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 添加 urlacl url=https://+:4443/WF user=domain\john doe

netsh http 添加 sslcert ipport=0.0.0.0:4443 certash=576a6bdddfeeb499b6b41d5d70d818755b483fc0 appid="{00000000-0000-0000-0000-000000000000}" 其中 certash 它是证书的缩略图。

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

我正在使用 .net 4.7。

【讨论】:

    猜你喜欢
    • 2022-12-22
    • 2015-01-09
    • 2014-05-11
    • 1970-01-01
    • 2022-12-19
    • 2014-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多