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.
精彩评论