How to get the ephemeral client port number in ASP.NET?

I would like to track client sessions by connection (not session state or cookies) in ASP.NET. I planned to do this using the client IP address and ephemeral port number .

You can get the IP address in ASP.NET through

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

None of them give you a response port number. Is there any other way to get it? Is it possible?

+4
source share
4 answers
 HttpContext.Current.Request.ServerVariables["REMOTE_PORT"]. ^^^^ 

perhaps?

+8
source

You cannot do this for normal web browsing, because the HTTP connections between the client and server are short-lived.

For example, IIS7 indicates a default idle timeout of 120 s, after which the connection will be dropped.

The next time your browser connects to the server, the “ephemeral” port number is likely to change (because it is randomly selected).

+1
source

Get the current HTTP context:
If you do this in the MVC controller, you can directly use the controller property HttpContext .

 var context = HttpContext.Current as IServiceProvider; 

Next, get an HTTP workstation request:

 var request = context.GetService( typeof(HttpWorkerRequest)) as HttpWorkerRequest; 

Then you can get the client ephemeral number as follows:

 var port = request.GetRemotePort(); 
+1
source

This works great.

 string portNo = HttpContext.Current.Request.ServerVariables["SERVER_PORT"]; 

You can visit this:

http://www.w3schools.com/asp/coll_servervariables.asp

-2
source

Source: https://habr.com/ru/post/1368861/


All Articles