gpt4 book ai didi

asp.net - 为 Asp.Net WEB API 2 自托管启用 Https

转载 作者:行者123 更新时间:2023-12-01 22:53:46 30 4
gpt4 key购买 nike

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

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

  • 从权威机构申请 Http 证书,并导入到 Windows keystore 。

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

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)

有什么想法吗?

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

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

最佳答案

我能够使用 WebServiceHost 对象来运行它来托管我的服务。这里是所有步骤:

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

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

2) 像这样配置服务 baseAddress 和绑定(bind):

<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="{00000000-0000-0000-0000-000000000000}" where certhash it the thumbnail of the certificate.

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

我正在使用 .net 4.7。

关于asp.net - 为 Asp.Net WEB API 2 自托管启用 Https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35500596/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com