Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get the ephemeral port number of a client in ASP .NET?

Tags:

.net

asp.net

tcp

I would like to track client sessions by connection (not session state or cookies) in ASP .NET. I was planning on doing this by using the client's IP address and ephemeral port number.

You can get the IPAddress in ASP .NET via

  • HttpContext.Current.Request.UserHostAddress
  • HttpContext.Current.Request.ServerVairables["REMOTE_ADDR"].

Neither of which give you the reply port number. Is there another way to get it? Is it even possible?

like image 619
Brandon Cuff Avatar asked Aug 24 '11 21:08

Brandon Cuff


2 Answers

HttpContext.Current.Request.ServerVariables["REMOTE_PORT"].
                                                    ^^^^

perhaps?

like image 195
Marc B Avatar answered Oct 28 '22 08:10

Marc B


You can't really do this for regular web browsing because HTTP connections between the client and the server are not long lived.

For example IIS7 specifies a default idle timeout of 120s after which the connection will be torn down.

The next time your browser connects to the server the "ephemeral" port number will most likely have changed (because it's chosen randomly).

like image 42
Kev Avatar answered Oct 28 '22 10:10

Kev