c# - 如何修复 'One or more errors occurred. (The SSL connection could not be established, see inner exception.)' ?

标签 c# ssl https asp.net-core-webapi

我创建了 web api,当尝试添加 ssl 时出现此错误,当代码尝试到达 web api 中的端点时出现此错误,这是一个自签名证书。

这是开发环境,使用 visual studio 2019 调试代码,但在尝试重新创建 ssl 证书后没有运气,检查了有关在 .net 核心应用程序中实现 https 的指南,但没有运气。

程序.cs:

 public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseUrls("http://*:5000")
                .UseSetting("https_port", "5001")
                .UseKestrel(options =>
                {

                    options.Listen(System.Net.IPAddress.Any, 5000);
                    options.Listen(System.Net.IPAddress.Any, 5001,
                        listenOptions => { listenOptions.UseHttps("localhost.pfx", "A2345_678b"); });
                })
                .UseStartup<Startup>();
    }

在 Startup.cs 中配置服务:

services.AddSignalR();
            services.AddMvc(options => options.EnableEndpointRouting = false).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
            services.AddSpaStaticFiles(configuration =>
            {
                configuration.RootPath = "ClientApp/build";
            });
            services.AddDefaultIdentity<IdentityUser>().AddRoles<IdentityRole>()
            .AddEntityFrameworkStores<SmartContext>();

             services.AddMvc(
                     options =>
                     {
                         options.SslPort = 5001;
                         options.Filters.Add(new RequireHttpsAttribute());

                     }
                 );
             services.AddHttpsRedirection(options =>
             {
                 options.RedirectStatusCode = StatusCodes.Status307TemporaryRedirect;
                 options.HttpsPort = 5001;
             });
             services.AddAntiforgery(
                     options =>
                     {
                         options.Cookie.Name = "_af";
                         options.Cookie.HttpOnly = true;
                         options.Cookie.SecurePolicy = CookieSecurePolicy.Always;

                         options.HeaderName = "X-XSRF-TOKEN";
                     }
                 );
             services.Configure<IdentityOptions>(options =>
             {
                 // Password settings
                 options.Password.RequireDigit = false;
                 options.Password.RequiredLength = 6;
                 options.Password.RequireNonAlphanumeric = false;
                 options.Password.RequireUppercase = false;
                 options.Password.RequireLowercase = false;

             });
            services.AddDbContext<SmartContext>(options =>
               options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));

最佳答案

您需要做的就是在您的项目根目录中运行以下命令:

dotnet dev-certs https --trust

这应该弹出一个对话框,询问您是否要将证书添加到您的受信任商店,您显然应该接受。您需要为每个项目执行此操作。例如,仅信任您的 Web 应用程序是不够的,如果该 Web 应用程序正在连接到 API 应用程序。您需要对 API 应用执行相同的操作,以便两个证书都可信。

关于c# - 如何修复 'One or more errors occurred. (The SSL connection could not be established, see inner exception.)' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57630672/

相关文章:

amazon-web-services - 如何将我的网站作为在 AWS 中作为 Docker swarm 集群运行的 https 运行?

internet-explorer - 如何在IE中处理和接受Self Signed SSL证书?

c#提取子字符串的最简单方法

c# - MVC4+EntityFramework架构

c# - LINQ 中 AsEnumerable() 的内部实现

ssl - 了解数字签名

c# - 线程同步c#

android - 在 Android 上创建 SSL 连接时出现 SSLPeerUnverifiedException

ssl - 如何使用haproxy解密和加密HTTPS流量?

azure - azure cloudapp.net 的 SSL 证书