Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Windows不接受自签名SSL证书_C#_Asp.net Core_Https_Asp.net Core 2.0_Kestrel Http Server - Fatal编程技术网

C# Windows不接受自签名SSL证书

C# Windows不接受自签名SSL证书,c#,asp.net-core,https,asp.net-core-2.0,kestrel-http-server,C#,Asp.net Core,Https,Asp.net Core 2.0,Kestrel Http Server,在本地,我使用IIS创建并导出了自签名证书。结果是一个PFX文件 我已经将其加载到我的ASP.NET核心解决方案中,并且正在像下面这样旋转Kestrel: var certificatePath = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), "cert.pfx")); var certificate = new X509Certificate2(certificatePath, "certpass"); Hos

在本地,我使用IIS创建并导出了自签名证书。结果是一个PFX文件

我已经将其加载到我的ASP.NET核心解决方案中,并且正在像下面这样旋转Kestrel:

var certificatePath = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), "cert.pfx"));
var certificate = new X509Certificate2(certificatePath, "certpass");

HostWeb = builder
    .UseKestrel(options =>
    {
        options.Listen(IPAddress.Loopback, 44321, listenOptions =>
        {
            listenOptions.UseHttps(certificate);
        });
    })
    .UseUrls("https://localhost:44321")
    .UseEnvironment("Test").Build();
HostWeb.Start();
当我在这个Web服务器上运行Chrome时,它仍然显示不安全

我错过了什么?还有什么我需要配置的吗


我认为您需要在操作系统级别注册此证书,以便将其视为有效证书。默认情况下,不会创建自签名证书。我使用了这个链接:

特别是该部分:

# import the pfx certificate
Import-PfxCertificate -FilePath $pfxFilePath Cert:\LocalMachine\My -Password $pfxPassword -Exportable

# trust the certificate by importing the pfx certificate into your trusted root
Import-Certificate -FilePath $cerFilePath -CertStoreLocation Cert:\CurrentUser\Root

# optionally delete the physical certificates (don’t delete the pfx file as you need to copy this to your app directory)
# Remove-Item $pfxFilePath
Remove-Item $cerFilePath
更新:您的代码正在设置安全连接初始化(握手)期间服务器将向客户端提供的证书。但是,您的客户必须能够识别此证书为有效证书,他需要信任它,信任发出此证书的机构。例如,您信任stackoverflow,因为您信任将其服务器提供给您的客户端的证书交付给您的权威DigiCert


更新2:关于以下主题的新文章:

从Chrome导出证书,方法是单击“不安全”,然后单击“证书”、“详细信息”选项卡,然后单击“复制到文件…”,然后选择一个文件将证书写入磁盘

然后,双击证书,然后单击“安装证书…”,将“存储位置”保留为“当前用户”,选择“将所有证书放置在以下存储中”,选择“受信任的根证书颁发机构”,然后完成向导

你得到的警告将消失


注意:这只能应用于开发环境。

我正在运行此服务器,作为UI测试集成的一部分。那么你是说仅仅在代码中包含证书是不够的?另外,单击“/!\浏览器中的“不安全”将告诉您浏览器拒绝证书的原因。这不是Kestrel的问题。这是一个操作系统的问题。操作系统,或者Firefox的浏览器,必须信任证书。Kestrel本身并不关心你给它什么证书,它假设你知道你在做什么。你确定证书没有使用过时的算法吗?如果不是(例如使用SHA-1),无论是否可信,Chrome都会将其显示为不安全。如果您想信任证书,Jexus Manager可以直观地执行此操作。下面的答案提供了等效的PowerShell脚本。@Robert,它取决于IIS版本。最新的IIS 10不会生成SHA2证书。