.net dotnet Web应用程序未在本地主机上运行:5000 / 50001

gcuhipw9  于 2023-03-20  发布在  .NET
关注(0)|答案(2)|浏览(165)

我尝试在visual studio中运行一个dotnet webapp项目。当我运行时,弹出一个powershell窗口,显示我的应用程序正在https://localhost:5000或localhost:5001上运行。但当我尝试访问它时,显示:无法找到此本地主机页面。browser screen-shot
powershell screen-shot

j9per5c4

j9per5c41#

如上面的Charles Mager注解所示
网络设备证书https --信任
这个答案对我很有效。

rqmkfv5c

rqmkfv5c2#

我和你有同样的问题。
可能是证件有问题。
因此,请尝试为这些端口运行此PowerShell脚本:

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=5000; $i -le 5002; $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
}

我希望这对你也有用!

相关问题