ASP.NET Core: An attempt was made to access a socket in a way forbidden by its access permissions

This morning I made some changes to a project for a future post. Minor stuff and nothing that should have caused issues, but when I tried to run the project I got the following error from Kestrel.

warn: Microsoft.AspNetCore.Server.Kestrel[0]
Unable to bind to https://localhost:5001 on the IPv4 loopback interface: ‘An attempt was made to access a socket in a way forbidden by its access permissions.’.
warn: Microsoft.AspNetCore.Server.Kestrel[0]
Unable to bind to https://localhost:5001 on the IPv6 loopback interface: ‘An attempt was made to access a socket in a way forbidden by its access permissions.’.
crit: Microsoft.AspNetCore.Server.Kestrel[0]
Unable to start Kestrel.
System.IO.IOException: Failed to bind to address https://localhost:5001.

I had limited time so I switched the project over to launch using IIS Express and hit run, but this resulted in the following error.

HTTP Error 500.35 – ANCM Multiple In-Process Applications in same Process

Switching back to Kestrel and changing the port number in the applicationUrl setting in the launchSettings.json file fixed the issue. Later I came back to the application and tried to track down what was using the same ports and couldn’t find anything. After a reboot, all worked fine with Kestrel and the original port numbers.

For the IIS Express error, I ended up having to delete the .vs directory which can be found in the same directory as your solution file if you are using Visual Studio 2019. Thanks to stackoverflow for this fix.

Wrapping Up

Not the post I set out to write this week, but hopefully this will save someone some searching in the future, more than likely myself.


Also published on Medium.

8 thoughts on “ASP.NET Core: An attempt was made to access a socket in a way forbidden by its access permissions”

  1. I was able to get it to work by changing this setting in the appsettings.json file. It changes the port on which the application runs. It worked very well for me.

    “Kestrel”: {
    “EndPoints”: {
    “Https”: {
    “Url”: “https://localhost:4300”
    }
    }
    }

Leave a Reply to RM Cancel Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.