本文介绍了启用Asp.Net WEB API 2自托管的Https的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它的费用我两天了,使我的web服务支持HTTPS,有趣的是,我无法找到如何实现这一共同要求的任何正式文件。

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:


  • 从应用授权一个Http证书,并导入到Windows密钥库。

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

现在我有2脚本命令,如:

Now I got 2 script commands like:

netsh的HTTP添加urlacl URL = 用户=每个人

netsh的HTTP添加的sslcert ipport = 0.0.0.0:9999 CERTHASH = XXXXXXXXXXXXXXXXXXXX的appid = {12345678-db90-4b66-8b01-88f7af2e36bf}

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


  • 修改code:

  • modify the code:

    string baseAddress = "https://+:9999/";
    try
    {
        // Start OWIN host
        using (WebApp.Start<SelfHostStartup>(url: baseAddress))
        {
             //.....
    


  • 运行该脚本2命令第一,看起来不错。运行我的应用程序,错误弹出:

  • 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)
    


  • 任何想法?

    [edit0]:
    通过使用的netstat -an ,我看不出端口 9999 在列表中。

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


    通过使用 netsh的HTTP删除urlacl URL = HTTP:// +:9999 / ,异常不见了,现在 9999 是在的netstat -an 听,我尝试使用IE浏览器访问HTTPS URL,它给我一个 500 错误。

    [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.

    推荐答案

    您必须启用通过以下步骤HTTPS
    1.左键单击​​项目,进入属性查看 - >启用SSL设置为TRUE,并复制项目路径是一般。然后右键单击应用程序的名称得属性 - >网络。变更项目URL和粘贴路径和保存。

    You have to enable https by following procedure1. Left click on project, go to properties view-> Enable ssl set=true and copy the project path which is generally https://localhost:44300. Then right click on application name got to properties-> Web. Change project Url and paste that path and save.

    这篇关于启用Asp.Net WEB API 2自托管的Https的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 23:35