c# - 有没有办法在 ASP.NET Core 3.1 应用程序端点中应用服务器 SSL 证书?

标签 c# asp.net ssl nginx certificate

我在我的 Ubuntu VPS 上配置了 Nginx。证书在前端端点 (*.html) 上完美运行,但我无法从任何客户端应用程序从我的 ASP.NET Core API 获取数据。例如,尝试使用以下 python 代码获取数据:

import requests
import json
data = {"somevariable" : "someData"}
url='https://myDNS:myPort/myEndpoint'
r = requests.post(url, json=data)

返回以下错误消息:

SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1108)'))



我的目标是使用来自任何客户端的 Nginx 生成的证书 (HTTPS) 与 ASP.NET 端点进行通信。我尝试了以下文档,但似乎都没有解决我的问题。
  • https://docs.microsoft.com/cs-cz/aspnet/core/host-and-deploy/linux-nginx?view=aspnetcore-3.1#configure-nginx
  • https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?tabs=aspnetcore2x&view=aspnetcore-3.1#when-to-use-kestrel-with-a-reverse-proxy

  • 应用程序启用了 SSL,并且 Program.cs 如下所示:
    public class Program
        {
            public static void Main(string[] args)
            {
                CreateHostBuilder(args).Build().Run();
            }
    
            public static IHostBuilder CreateHostBuilder(string[] args) =>
                Host.CreateDefaultBuilder(args)
                    .ConfigureWebHostDefaults(webBuilder =>
                    {
                        //webBuilder.UseUrls("http://0.0.0.0:5001");
                        webBuilder.UseStartup<Startup>();
                        webBuilder.UseKestrel(options =>
                        {
                            options.Listen(IPAddress.Loopback, 5000); //Should listen on https://MyDNS:5000/
                        });
                    });
        }
    

    我还在 Startup.cs 的同一端口上使用 HttpsRedirection,如下所示:
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
                app.UseHttpsRedirection();
                //.... rest of configuration
            }
     public void ConfigureServices(IServiceCollection services)
            {
                //...
                services.AddHttpsRedirection(options =>
                {
                    options.RedirectStatusCode = StatusCodes.Status307TemporaryRedirect;
                    options.HttpsPort = 5000;
                });
                //...
          }
    

    我会非常感谢任何答案。

    最佳答案

    我认为这是 nginex 配置问题。
    尝试将配置修改为:

    server 
    { 
        listen 443 ssl; 
        server_name api.vojtechpetrasek.com; 
    
        location / {
    
          proxy_set_header        Host $host;
          proxy_set_header        X-Real-IP $remote_addr;
          proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header        X-Forwarded-Proto $scheme;
    
          # Fix the “It appears that your reverse proxy set up is broken" error.
          proxy_pass          http://localhost:5000;
          proxy_read_timeout  90;
    
          proxy_redirect      http://localhost:5000 https://api.vojtechpetrasek.com;
        }
    
    #.... managed by Certbot 
    } 
    

    您的 asp.net 应用程序应该在 http://localhost:5000 上运行
    (在linux上可以通过“链接http://localhost:5000”进行测试)

    好教程
    https://www.digitalocean.com/community/tutorials/how-to-configure-nginx-with-ssl-as-a-reverse-proxy-for-jenkins

    关于c# - 有没有办法在 ASP.NET Core 3.1 应用程序端点中应用服务器 SSL 证书?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60365156/

    相关文章:

    c# - Microsoft Azure 移动服务 - Xamarin.Android 离线同步问题

    xcode - SSL 使用 x.509 证书 cocoa

    c# - MVC 4 - JSON ajax 操作结果的 GZIP 压缩

    c# - ASP.NET MVC3中不同区域的自定义错误页面

    asp.net - Visual Studio 2005 中缺少 "Export Template"选项

    返回 void 的 ASP.Net MVC Controller 操作

    c# - 尝试在 formview 中绑定(bind)下拉列表时出现空引用异常

    amazon-web-services - 如何在 kubernetes 中将 SSL 证书安装到 aws 负载均衡器?

    SSL23_GET_SERVER_HELLO :unknown protocol

    c# - Regex 如何获取 url() 的内容