I am using the following code. While running in localhost it gives correctly the client machine name, but when i run it the development server, it is not giving the actual result. any idea?
This is an asp.net mvc application.
string clientMachineName;
clientMachineName =
(Dns.GetHostEntry(Request.ServerVariables["remote_addr"]).HostName);开发者_如何学编程
Response.Write(clientMachineName);
I want the computer name or machine name of the client that is making request. The above code gives me an value '172.16.12.100'.
any help much appreciated.
You do realize that, what you are asking and how you are doing is just wrong right?
You need to understand what's an IP Address in the first place.
When using Request.ServerVariables("REMOTE_ADDR")
this would never be the "real address",
since no "real address" is ever sent.
An IP address is something used at the network layer, and shouldn't really be used by your application, if you really want the identity of a given machine, you need to use something like X.509 certificates, which are meant to represent an identity (an IP address is not an identity).
You will have problems when a user is behind a NAT, and you will never get the correct identity of an user.
That is why under localhost everything works fine but soon you want something outside your "box" everything starts to go bad.
The Dns.GetHostEntry Method (String)
method queries a DNS server for the IP address that is associated with a host name or IP address. This would probably not work on a remote network because you aren't querying the same DNS server.
More info: http://msdn.microsoft.com/en-us/library/ms143998.aspx
You aren't guaranteed to get that information on a remote network. I'm assuming your dev server is not on the same network.
精彩评论