c# - 在 IISExpress 上运行的 ASP.NET Core 应用程序中的 HTTPS 错误 - PR_CONNECT_RESET_ERROR

标签 c# asp.net-core ssl ssl-certificate iis-express

当我尝试在本地运行此应用程序时(启用 SSL 时),
enter image description here
我总是得到这个提示安全连接的页面:
https://localhost:44300/

Secure Connection Failed

An error occurred during a connection to localhost:44300. PR_CONNECT_RESET_ERROR

  1. The page you are trying to view cannot be shown because the authenticity of the received data could not be verified.
  2. Please contact the website owners to inform them of this problem.

到目前为止我已经尝试过:
  • 添加自签名localhost带有此 Power Shell 命令的证书:
  • New-SelfSignedCertificate -DnsName "localhost" -CertStoreLocation "cert:\LocalMachine\My"
  • 运行mmc.exe并导出 certificate由上述 Power Shell 脚本从
  • 创建
    [Console Root\Certificates (Local Computer)\Personal\Certificates][Console Root\Certificates (Local Computer)\Trusted Root Certification Authorities\Certificates .
    到目前为止,这还没有奏效。如果我运行应用程序取消选中启用 SSL,它工作正常。
    请就启用 SSL 的环境的可能解决方案提供建议。

    最佳答案

    重新生成 IIS Express localhost证书对我有用:

  • 打开Windows PowerShell ISE使用管理员权限。
  • 运行此脚本:
  • Start-Transcript -Path "$($MyInvocation.MyCommand.Path).log"
    try {
        Write-Host "Creating cert resources"
        $ekuOidCollection = [System.Security.Cryptography.OidCollection]::new();
        $ekuOidCollection.Add([System.Security.Cryptography.Oid]::new("1.3.6.1.5.5.7.3.1","Server Authentication")) | Out-Null
        $sanBuilder = [System.Security.Cryptography.X509Certificates.SubjectAlternativeNameBuilder]::new();
        $sanBuilder.AddDnsName("localhost") | Out-Null
        
        Write-Host "Creating cert extensions"
        $certificateExtensions = @(
            # Subject Alternative Name
            $sanBuilder.Build($true),        
            # ASP.NET Core OID
            [System.Security.Cryptography.X509Certificates.X509Extension]::new(
                "1.3.6.1.4.1.311.84.1.1",
                [System.Text.Encoding]::ASCII.GetBytes("IIS Express Development Certificate"),
                $false),
                # KeyUsage
                [System.Security.Cryptography.X509Certificates.X509KeyUsageExtension]::new(
                    [System.Security.Cryptography.X509Certificates.X509KeyUsageFlags]::KeyEncipherment,
                    $true),
                    # Enhanced key usage
            [System.Security.Cryptography.X509Certificates.X509EnhancedKeyUsageExtension]::new(
                $ekuOidCollection,
                $true),
                # Basic constraints
                [System.Security.Cryptography.X509Certificates.X509BasicConstraintsExtension]::new($false,$false,0,$true)
            )
        Write-Host "Creating cert parameters"
        $parameters = @{
            Subject = "localhost";
            KeyAlgorithm = "RSA";
            KeyLength = 2048;
            CertStoreLocation = "Cert:\LocalMachine\My";
            KeyExportPolicy = "Exportable";
            NotBefore = Get-Date;
            NotAfter = (Get-Date).AddYears(1);
            HashAlgorithm = "SHA256";
            Extension = $certificateExtensions;
            SuppressOid = @("2.5.29.14");
            FriendlyName = "IIS Express Development Certificate"
        }
        Write-Host "Creating cert"
        $cert = New-SelfSignedCertificate @parameters
    
        $rootStore = New-Object System.Security.Cryptography.X509Certificates.X509Store -ArgumentList Root, LocalMachine
        $rootStore.Open("MaxAllowed")
        $rootStore.Add($cert)
        $rootStore.Close()
        
        Write-Host "Creating port bindings"
        # Add an Http.Sys binding for port 44300-44399
        $command = 'netsh'
        for ($i=44300; $i -le 44399; $i++) {
            $optionsDelete = @('http', 'delete', 'sslcert', "ipport=0.0.0.0:$i")
            $optionsAdd = @('http', 'add', 'sslcert', "ipport=0.0.0.0:$i", "certhash=$($cert.Thumbprint)", 'appid={214124cd-d05b-4309-9af9-9caa44b2b74a}')
            Write-Host "Running $command $optionsDelete"
            & $command $optionsDelete
            Write-Host "Running $command $optionsAdd"
            & $command $optionsAdd
        } 
    }
    catch {
        Write-Error $_.Exception.Message
    }
    finally {
        Stop-Transcript
    }
    
    
    它现在应该可以正常工作了。
    (脚本来自这个 Github 问题页面的@Shirhatti:https://github.com/dotnet/aspnetcore/issues/26437)

    关于c# - 在 IISExpress 上运行的 ASP.NET Core 应用程序中的 HTTPS 错误 - PR_CONNECT_RESET_ERROR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64880715/

    相关文章:

    google-chrome - 自签名 SSL 证书不适用于 MAMP 和 Chrome

    sql-server - .NET 5 + Microsoft.Data.SqlClient - 从传输流接收到意外的 EOF 或 0 字节

    C# WPF Datagrid 如何禁用特定列上的选择单元格

    c# - C#中的 `Fields`和 `Properties`有什么区别?

    c# - 如何将组合框绑定(bind)到 WPF 中的多语言值集合?

    c# - .net 中的数据同步设计

    c# - 全新 ASP.NET Core 2.1 项目的构建时间缓慢

    ssl - 在 kafka 生产者中使用/127.0.0.1(SSL 握手失败)(org.apache.kafka.common.network.Selector)认证失败

    c# - 如何从模型中获取图像以在 CSS 样式标签的 View 中使用?

    c# - ClaimType 的 ASP.NET 要求