Determine Client's Computer Name - c#

I am building an intranet site that will display different lists based on the computer name because different computers are in different areas, is there a way (within a controller or model) to determine the client's computer name?
I have tried system.environment.machinename but that only returns the name of the server, any other ideas?

I got it working using the following:
string IP = Request.UserHostName;
string compName = CompNameHelper.DetermineCompName(IP);
code from compnamehelper:
public static string DetermineCompName(string IP)
{
IPAddress myIP = IPAddress.Parse(IP);
IPHostEntry GetIPHost = Dns.GetHostEntry(myIP);
List<string> compName = GetIPHost.HostName.ToString().Split('.').ToList();
return compName.First();
}

code in VB :
Dim myIP As IPAddress = IPAddress.Parse(Request.UserHostName)
Dim GetIPHost As IPHostEntry = Dns.GetHostEntry(myIP)
Dim compName As List(Of String) = GetIPHost.HostName.ToString.Split("").ToList
return(compName.First)

No. The client's computer name is not available in any way on the server. This is the nature of the http request-response. You only can have its IP address.
A workarounds could be to retrieve machine on the client from Flash/Silverlight (I doubt JavaScript) and put in into cookie which is available on the server with each request. But there is a whole stack of issues with this approach.

I think you are better off using one of these methods to tie a user to a location:
a cookie that is set once the user self-selects their location
having the user login to the site so that you can track them uniquely that way
remembering user by IP address
There is no way of ensuring remote hostnames are unique. The same issue occurs with IP because of proxies, dynamic IP, etc., but I think it will be a little more reliable. Also, you can do geolocation by IP address.

Try this:
string name = Request.UserHostName;

The only way I know of to inspect the client is through the ServerVariables collection on the Request object (should be available for MVC code).
See https://web.archive.org/web/20210927201638/http://www.4guysfromrolla.com/webtech/092298-3.shtml for more information. REMOTE_HOST and REMOTE_ADDR look like candidates.

Here's an IE-only solution. It works in IE8, with multiple security warnings.
<script type="text/javascript" language="javascript">
var ax = new ActiveXObject("WScript.Network");
document.write(ax.UserName + '<br />'); //logged in account name
document.write(ax.ComputerName + '<br />'); //Windows PC name
</script>

Related

Get User's IP Address in a Share Point Web Part not giving valid IP address

This code in a standard custom WebPart:
private string GetUserIP()
{
return Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ?? Request.ServerVariables["REMOTE_ADDR"];
}
is returning: "fe80::c564:7922:d873:5cf5%11" instead of a valid IP address. It does this for every method I've found on Google for retrieving it, including HttpRequest.UserHostAddress.
Does anyone have an idea what is going on?
Edit: For some reason it's giving me IPv6 when loading the page locally, but works as intended when I access from a different machine :/
Use this,
string ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(ip))
{
ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
Or use
string ipAddress = Request.UserHostAddress

How to Get IPAddress when User open web site using IP Address?

I am generating run time <a> link. To complete link I am using bellow code:
string appPath = protocol + System.Web.HttpContext.Current.Request.ServerVariables["HTTP_HOST"] + System.Web.HttpContext.Current.Request.ApplicationPath;.
But when user try to open web site from: http://123.123.123.123/testApp at this time my link is created with http://myservername.com/testApp.
I want the address what ever user enter.
If user Open open website from http://123.123.123.123/testApp the link should be
http://123.123.123.123/testApp/Default.aspx
and If User open website from http://myservername.com/testApp the link should be
http://myservername.com/testApp/Default.aspx
Use REMOTE_ADDR server variable
string appPath = protocol +
System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"] +
System.Web.HttpContext.Current.Request.ApplicationPath;
One of HTTP_HOST or SERVER_NAME should provide you with information on what user typed to get to your site. It may be good idea to check with Http debugger what browser actually sends in case of IP address to make sure you check correct values.
`HttpContext.Current.Request.ServerVariables("HTTP_HOST");`
Use this function to get the IPAddress of the user.
But there is a possibility that you may not get the actual IP. There are many concern like firewall etc. Which may expose same ip for all the computer on the network.
public static string GetMachineName(HttpRequest moRequest)
{
return moRequest.ServerVariables["HTTP_X_FORWARDED_FOR"] != null ||
moRequest.ServerVariables["HTTP_CLIENT_IP"] != null
? moRequest.ServerVariables["HTTP_X_FORWARDED_FOR"]
: moRequest.ServerVariables["REMOTE_ADDR"];
}

How to get the computer name (hostname in a web aplication)?

how can I get the client's computer name in a web application. The user in a network.
Regards
// Already tryed this option
string IP = System.Web.HttpContext.Current.Request.UserHostAddress;
string compName = DetermineCompName(IP);
System.Net.IPHostEntry teste = System.Net.Dns.GetHostEntry(IP);
ssresult = IP + " - " + teste.HostName;
// TODO: Write implementation for action
private static string DetermineCompName(string IP)
{
IPAddress myIP = IPAddress.Parse(IP);
IPHostEntry GetIPHost = Dns.GetHostEntry(myIP);
string[] compName = GetIPHost.HostName.ToString().Split('.');
return compName[0];
}
All of that, gives me only the IP :/
You can't do this in a way that is guaranteed to work.
The closest you will be able to get is to go down the route of doing a reverse dns lookup using System.Net.Dns.GetHostEntry which is what you have already tried.
The problem is that your machine has no way of knowing the hostname of a remote web client via its IP address alone (unless it is on the same subnet, in which case you may be able to retrieve it).
You have to fall back on your DNS infrastructure being able to map the IP back into a hostname [this is what nslookup does when you type in a hostname], and plenty of places just won't bother setting up reverse IP records.
Plus, often if they do, they won't match the hostname. It is quite common to see a reverse lookup for "1.2.3.4" come back as something line "machine-1.2.3.4", instead of the actual hostname.
This problem is exacerbated further if the clients are behind any sort of Network Address Translation so that many client computers have a single IP from an external perspective. This is probably not the case for you since you state "The user in a network".
As an aside, if you go to the server and type in, at a command prompt,
nslookup <some ip>
(where is an example of one of these client machines), do you get a hostname back?
If you do, then System.Net.Dns.GetHostEntry should be able to as well, if not then it probably can't for the reasons mentioned above.
you can get the windows machine name with - System.Environment.MachineName
Assuming your clients are Windows based running IE you could use this client side code to get the names and pass them back to the server:
<script type="text/javascript">
function create()
{
var net = new ActiveXObject("wscript.network");
document.write(net.ComputerName);
}
</script>
Yeah would require you to keep requesting and caching the computers terribly inefficient, was a bad idea.
I would go with running nslookup in the background I haven't tested this code and neither is it handling errors for failures, but basically, you can do:
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo sInfo = new System.Diagnostics.ProcessStartInfo("nslookup.exe", "192.168.1.100");
string result = process.StandardOutput.ReadToEnd();
Then just parse the result value.

asp.net c# ip address lost after postback?

One of my functions in a class is called GetIpAddress() which returns the following string:
System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]
This all works well in regular page loads and gets my ip address, but when i for example let a user place a comment, then the ip address is lost after postback and i get an empty string returned.
Am I missing something here maybe?
Kind regards,
Mark
Use these ones:
HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; // a user is going through a proxy server
HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
HttpContext.Current.Request.UserHostAddress;
Some explanation:
REMOTE_ADDR The IP address of the remote host making the request.
REMOTE_HOST The name of the host making the request. If the server does not have this information, it will set REMOTE_ADDR and leave this empty.
Do you have it wrapped in something like this?
if (!Page.IsPostBack) {
GetIpAddress()
}
You need to either call the function every time, or store it in a Shared variable.
Another option (As #Marc mentioned) would be to simply call Request.UserHostAddress instead of GetIpAddress... it's almost the same amount of typing, but you don't have to have a custom function for it.

Getting ipaddress and location of every user visiting your website

How do you get the ipaddress and location of every website vistor of your website through Asp.Net?
Thanks
To get the user's IP use:
Request.UserHostAddress
You can use this webservice to get their geographic location.
http://iplocationtools.com/ip_location_api.php
string VisitorIPAddress = Request.UserHostAddress.ToString();
and based on the ipaddress you can narrow down the location: find the geographical location of a host
Request.UserHostAddress won’t work if you're behind a proxy. Use this code:
public static String GetIPAddress()
{
String ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(ip))
ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
else
ip = ip.Split(',')[0];
return ip;
}
Note that HTTP_X_FORWARDED_FOR should be used BUT as it can return multiple IP addresses separated by a comma you need to use the Split function. See this page for more info.
Well the following property should give you the IP Address of teh client (or the clients proxy server)
Request.UserHostAddress
As for location, you'd need to use some GeoIP/GeoLocation plugin like MaxMind to figure that out.
http://www.maxmind.com/
To get the IP do:
Request.UserHostAddress
And you can map IP to location using a webservice (slower) or a database (faster) like this:
http://ip-to-country.webhosting.info/node/view/5
It's server technology-agnostic, but I'd recommend piggy-backing on Google's AJAX loader: http://code.google.com/apis/ajax/documentation/#ClientLocation
It's in Javascript and will even give you the person's city/state/country (well, it takes a guess based on IP address). Post it back to the server and it's available to you in ASP.NET or whatever.

Categories

Resources