Search code examples
visual-studioasp.net-corehttpsiis-express

Visual Studio ASP.NET Core Debug IIS Express - Site can't be reached


I've been working on an ASP.NET Core 2.1 web application in VS 2019 on a Windows 10 PC. Everything was working fine and no issue with debugging in Chrome/IIS Express.

I copied my project to a new Win 10 computer that I've just started to use.

When I try to debug the project with IIS Express in Chrome, I get an error page that says: This site can't be reached.

I followed all kinds of suggested fixes from Overflow articles but nothing seems to work. This is driving me mad, wasting so much time. I've tried all these steps but no joy:

  • disable anti-virus firewall/protection
  • check project properties (same as previous PC)
  • check launchSettings.json file (same as previous PC)
  • delete applicationhost.config in vs/config folder (post said this file will be recreated on rebuild but after rebuild file is still not there!)
  • delete obj folder and rebuild
  • delete vs folder and rebuild
  • Check Windows Features (same as old PC which is working fine)
  • change VS Options/Debugging: Uncheck the Enable Edit and Continue check box
  • run cmd as administrator: cd "C:\Program Files (x86)\IIS Express" IisExpressAdminCmd.exe setupsslUrl -url:https://localhost:44301/ -UseSelfSigned

launchSettings.json file

launchSettings.json
{
  "iisSettings": {
    "windowsAuthentication": true,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "https://localhost:44301/",
      "sslPort": 44376
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "CanvasWeb": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "http://localhost:61900/"
    }
  }
}

Project Properties

UPDATE

I tried the following actions but still not resolved the problem:

  • ran repair of IIS Express
  • checked no duplicate certificates in cert store
  • deleted certificate to make IIS generate new one
  • created a new "test" project - same problem occurs

Solution

  • The issue may be HTTPS Error with IIS Express. To check it: create a new ASP.NET Core Web Application without Https. Uncheck /deselect configure for https in the Web Application template Uncheck configure for https

    If the new web application runs, then it is IIS Express HTTPS issue. running the flowing power shell code with administrator previliadge will solve IIS Express issue.

    # code start
    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
    }
    # code End

    Reference: HTTPS Error using IIS Express #26437