An example of obtaining the real IP address of a remote client (for reference only ):
View code
1 public static string getip ()
2 {
3 string strip = string. empty;
4 // obtain the real IP address of the proxy user
5 // if it is not a proxy user, null is returned.
6 strip = httpcontext. Current. Request. servervariables ["http_x_forwarded_for"];
7 if (string. isnullorempty (strip ))
8 {
9 // obtain the IP Address requested by the remote client
10 strip = httpcontext. Current. Request. servervariables ["remote_addr"];
11}
12 if (string. isnullorempty (strip ))
13 {
14 // obtain the IP address of the remote client host
15 strip = httpcontext. Current. Request. userhostaddress;
16}
17 if (string. isnullorempty (strip )&&! Isip (strip ))
18 {
19 Return "0.0.0.0 ";
20}
21
22 return strip;
23}
24
25 public static bool isip (string strip)
26 {
27 // verify the IP Format
28 return RegEx. ismatch (strip, @ "^ (2 [0-4] \ d | 25 [0-5] | [01]? \ D ?) \.) {3} (2 [0-4] \ d | 25 [0-5] | [01]? \ D ?) $ ");
29}
--------------------------------------------------------- Welcome to learn and exchange ----------------------------------------------------------