Bar*_*rez 0 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>();
}
Run Code Online (Sandbox Code Playgroud)
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")));
Run Code Online (Sandbox Code Playgroud)
您需要做的就是在项目根目录中运行以下命令:
dotnet dev-certs https --trust
Run Code Online (Sandbox Code Playgroud)
这应该会弹出一个对话框,询问您是否要将证书添加到受信任的存储中,您显然应该接受。您需要为每个项目执行此操作。例如,如果 Web 应用程序连接到 API 应用程序,仅信任您的 Web 应用程序是不够的。您需要为 API 应用程序执行相同的操作,以便信任这两个证书。
| 归档时间: |
|
| 查看次数: |
7116 次 |
| 最近记录: |