【发布时间】: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,还是一样的错误。看到我的edit0,netstat -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