0

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:

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?

Alon M
  • 1,483
  • 7
  • 28
  • 42
  • See if this can help you: http://stackoverflow.com/questions/735350/how-to-get-a-users-client-ip-address-in-asp-net. – Fred Aug 03 '11 at 15:01

2 Answers2

1

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.

Mrchief
  • 73,270
  • 19
  • 138
  • 185
0

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.

Barry Kaye
  • 7,442
  • 6
  • 39
  • 62
  • i am have tryed do to this :protected void Page_Load(object sender, EventArgs e) { Session["Id"] = HttpContext.Current.Request.UserHostAddress; } , its not working when i am dogin :writer.WriteLine(Session["Id"].ToString()); – Alon M Aug 03 '11 at 15:18