12

When specifying a port to bind to with .UseKestrel() I get the errors listed below.. but if I remove the kestrel options everything works normally if I check the API from my browser.

I've tried binding to the port that my application defaults to with no ports chosen and I've tried checking netstat to actively avoid any ports that are in use. Nothing works but removing the options entirely. This is not replicated on my Mac or another Windows 10 machine. This device is Windows 10.

.UseKestrel(options =>
{
    options.Listen(IPAddress.Loopback, 50470);
    options.Listen(IPAddress.Any, 80);
})

: Microsoft.AspNetCore.Server.Kestrel[0]
Overriding address(es) 'http://localhost:50470/'. Binding to endpoints defined 
in UseKestrel() instead.
crit: Microsoft.AspNetCore.Server.Kestrel[0]
Unable to start Kestrel.
System.Net.Sockets.SocketException (10013): An attempt was made to access a 
socket in a way forbidden by its access permissions
at 
System.Net.Sockets.Socket.UpdateStatusAfterSocketErrorAndThrowException
(SocketError error, String callerName)
at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress 
socketAddress)
at System.Net.Sockets.Socket.Bind(EndPoint localEP)
at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketTransport.
BindAsync() at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer. 
<>c__DisplayClass21_01.<<StartAsync>g__OnBind|0>d.MoveNext() ` 
static_cast
  • 1,174
  • 1
  • 15
  • 21
Ping Pong
  • 139
  • 1
  • 1
  • 4
  • Is it the same user on both Windows machines? "...forbidden by its access permissions" could point to a group policy setting. – Duston Feb 13 '19 at 14:50
  • It is not, they are different computers entirely and another person running the code. – Ping Pong Feb 13 '19 at 15:23

7 Answers7

27

Also check Darkthread's answer here: https://superuser.com/questions/1486417/unable-to-start-kestrel-getting-an-attempt-was-made-to-access-a-socket-in-a-way

We discovered that a port we had been using for a long time wasn't acccessible anymore because it had been reserved by Windows! You may wish to check reserved ports using this command: netsh interface ipv4 show excludedportrange protocol=tcp

norgie
  • 497
  • 7
  • 13
  • 1
    Had this same problem and this was the only thing that helped me hunt it down. Why is Windows now setting such large ranges of port reservations? – slasky Mar 17 '20 at 18:27
  • 1
    Yes this was the only thing that helped. Microsoft should document this. – pantonis Mar 27 '20 at 09:17
  • https://superuser.com/questions/1579346/many-excludedportranges-how-to-delete-hyper-v-is-disabled – RouR Jul 16 '22 at 08:04
6

After Windows Update, some ports are reserved by Windows and applications cannot bind to these ports. please check this command for forbiden port on Os

netsh interface ipv4 show excludedportrange protocol=tcp

Omid Rahimi
  • 457
  • 4
  • 7
2

In my case, Removing invalid local IP and Port address combinations from the app's launchSettings.json did it.

Mr. Kone
  • 49
  • 3
1

When you run an ASP.NET Core application directly through Kestrel, without an additional reverse proxy like IIS or nginx, you will need to configure the hosting URL properly.

The problem was because, you did not follow Port sharing limitation in Kestrel web server.

When you to use the Kestrel web server, you should set unique port to app. (if you use the port 80, make sure no apps use this port). and your app has enough permissions, too.

more info:

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?view=aspnetcore-2.2

I hope is useful.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
AminRostami
  • 2,585
  • 3
  • 29
  • 45
1

The additional binding of port 80 in ".UseKestrel(options => { options.Listen(...) })" was causing the issue in my case.

Andy
  • 35
  • 3
0

Try Updating The Https Certificate with this command. It worked for me!!

dotnet dev-certs https
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 08 '22 at 20:36
-1

The problem was with the additional binding of port 80, updating this corrected the problem.

Ping Pong
  • 139
  • 1
  • 1
  • 4
  • 1
    are you able to explain what did you do to fix this a little better? Where did you update the binding? on the IIS? – tonycdp May 17 '19 at 14:48