开发者

Asp.NET|Get Ip Of a user when he open the site\click on a button

开发者 https://www.devze.com 2023-03-25 04:27 出处:网络
Simple as that. i want to save the ip on a session id, or. when he click on a button it will do like that:

Simple as that.

i want to save the ip on a session id, or. when he click on a button it will do like that:

开发者_C百科using (StreamWriter writer = new StreamWriter(fileStream))
        {
          writer.WriteLine(TextBox1.Text);
          }

(its not all the code ofc)

when he click on a button, it will wirte the ip into the file. :)

any way to do so?


Use:

HttpContext.Current.Request.UserHostAddress; 

or

HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

Also, you might first want to check if he's behind a proxy:

if(String.IsNullOrEmpty(HttpContext.Current.Request.ServerVariables("HTTP_X_FORWARDED_FOR"))) {
   string ipAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
else {
   string ipAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

}

And I would agree to @Barry's post about this not being a perfect solution.


To get the user's IP address use Request.UserHostAddress although please note that this is not a perfect solution as it will not show individual user's where for example they are on corporate network behind a firewall and only exposing one (or more) external company IPs.

0

精彩评论

暂无评论...
验证码 换一张
取 消