无法使用HTTPS启动ASP.NET核心

无法使用HTTPS启动ASP.NET核心,asp.net,asp.net-core,iis-express,asp.net-core-webapi,Asp.net,Asp.net Core,Iis Express,Asp.net Core Webapi,我有一个WPF应用程序,它启动一个ASP.NET核心WEB API应用程序 当我使用这些配置启动WEB API项目作为启动项目时,它可以很好地用于HTTPS。 但是,当我尝试从WPF环境启动此应用程序时,它不适用于HTTPS 配置: Web API配置: 在Startup.cs文件中: public void配置服务(IServiceCollection服务) { services.AddMvc(); 配置(选项=> { options.Filters.Add(新的requireHttpAtt

我有一个WPF应用程序,它启动一个ASP.NET核心WEB API应用程序

当我使用这些配置启动WEB API项目作为启动项目时,它可以很好地用于HTTPS。 但是,当我尝试从WPF环境启动此应用程序时,它不适用于HTTPS

配置:

  • Web API配置:
  • 在Startup.cs文件中:
  • public void配置服务(IServiceCollection服务)
    {
    services.AddMvc();
    配置(选项=>
    {
    options.Filters.Add(新的requireHttpAttribute());
    });
    }
    
    主要方法如下所示:

    publicstaticvoidinithttpserver()
    {
    var host=new WebHostBuilder()
    .UseKestrel()
    .UseContentRoot(目录.GetCurrentDirectory())
    .Useii整合()
    .UseStartup()
    .useURL(“https://localhost:44300/")
    //.UseApplicationInsights()
    .Build();
    host.Run();
    }
    
    当我使用netstat命令检查端口时,它显示:

    邮递员说:

    未命中应用程序中操作方法上的调试器

    附言: 当我恢复对HTTPS的更改并尝试使用HTTP时,它工作正常


    HTTP的主要方法具有不同的端口,并且没有上述任何配置更改。

    当您在web服务器设置中启用SSL时,您将为IIS启用SSL,而不是应用程序。当您从VisualStudio启动web API时,它将作为反向代理服务在IIS后面运行。这就是为什么只有在将SSL作为启动项目运行时才能获得SSL。当您从WPF应用程序运行它时,API仅在Kestrel上运行

    因此,要在Kestrel上启用SSL,您需要添加一个证书,然后在设置Kestrel时将其传入

    var cert = new X509Certificate2("YourCert.pfx", "password");
    
    var host = new WebHostBuilder()
        .UseKestrel(cfg => cfg.UseHttps(cert))
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .UseStartup<Startup>()
        .UseUrls("https://localhost:44300/")
        //.UseApplicationInsights()
        .Build();
    
    var cert=new X509Certificate2(“YourCert.pfx”,“密码”);
    var host=new WebHostBuilder()
    .UseKestrel(cfg=>cfg.UseHttps(证书))
    .UseContentRoot(目录.GetCurrentDirectory())
    .Useii整合()
    .UseStartup()
    .useURL(“https://localhost:44300/")
    //.UseApplicationInsights()
    .Build();
    
    投票支持清晰的描述!我认为您需要在某个地方配置应该使用哪个证书。当我作为启动项目运行Web API应用程序时,它工作正常,不需要证书。从某个WPF应用程序内部启动时是否需要一个?我花了一些时间生成一个.pfx并在浏览器上为邮递员配置它。但是马上就成功了!谢谢如果我想的话,有没有一种方法可以通过这个设置(从WPF内部)使用IIS?是的,你可以。您需要从WPF应用程序启动IIS,并将其配置为侦听端口44300(或您分配给它的任何端口)并启用SSL。我不知道该怎么做。祝你好运
    public static void InitHttpServer()
        {
            var host = new WebHostBuilder()
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIISIntegration()
                .UseStartup<Startup>()
                .UseUrls("https://localhost:44300/")
                //.UseApplicationInsights()
                .Build();
    
            host.Run();
        }
    
    var cert = new X509Certificate2("YourCert.pfx", "password");
    
    var host = new WebHostBuilder()
        .UseKestrel(cfg => cfg.UseHttps(cert))
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .UseStartup<Startup>()
        .UseUrls("https://localhost:44300/")
        //.UseApplicationInsights()
        .Build();