Click here to Skip to main content
16,022,896 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi Everybody,

Actually there is a website and users are entering into that site by logging in. Now, I like collect the user's MachineName and IP address.

I applied several ways but those all give me back the Server's information rather Proxy Server's Info, not the Client's machine info.

I would be highly appreciated if any one of you kindly revert me back with the possible solutions.

The Code snippets Those I tried upon are:
C#
string strDNS = Dns.GetHostName();
string aa = System.Security.Principal.WindowsIdentity.GetCurrent().User.ToString().Trim();
string bb = System.Security.Principal.WindowsIdentity.GetCurrent().Name.Substring(5).ToString().Trim();

//Retrieving Server IPAddress                    
string strHostName = System.Net.Dns.GetHostName();
string strHostIPAddress = System.Net.Dns.GetHostAddresses(System.Net.Dns.GetHostName()).GetValue(0).ToString();

string a = System.Net.Dns.GetHostEntry(Request.ServerVariables["remote_addr"]).HostName;
string b = this.Page.Request.ServerVariables["REMOTE_ADDR"];
string c = Request.UserHostAddress;
string d = Server.HtmlEncode(Request.UserHostName);

//Get The User Ip Address
string ipAddress = GetUserIPAddress();
string str = Request.Form["hdn"];
string MyUserName = Environment.UserName;
string MyDomainName = Environment.UserDomainName;
string MyComputerName = System.Net.Dns.GetHostName();
string MyIPAddress = System.Net.Dns.GetHostEntry(MyComputerName).AddressList[0].ToString();

//Retrieving Client Machine details
System.Net.IPAddress[] strClientIPAddress = System.Net.Dns.GetHostAddresses(Environment.MachineName);
string strClientMachineName = Environment.MachineName.ToString().Trim();
string strClientUserName = Environment.UserName.ToString().Trim();
string strClientDomainName = Environment.UserDomainName.ToString().Trim();
string strClientOSVersion = Environment.OSVersion.ToString().Trim();

string[] computer_name = System.Net.Dns.GetHostEntry(Request.ServerVariables["remote_addr"]).HostName.Split(new Char[] { '.' });
computer_name[0].ToString();

string getUserHostAddress = HttpContext.Current.Request.UserHostAddress;
string getUserHostName = HttpContext.Current.Request.UserHostName;

Thanks,
Saswata


[Corrected code formatting]
Posted
Updated 25-Feb-10 23:04pm
v2

1 solution

All but the last two lines of code supplied failed as the asp.net code runs on the server, so gets the server IP & hostname

C#
string getUserHostAddress = HttpContext.Current.Request.UserHostAddress;
string getUserHostName = HttpContext.Current.Request.UserHostName;


Were close, but HttpContext.Current.Request.UserHostAddress
Referes to the local (ie server machine).


This shoul get what you need:

C#
Request.ServerVariables("REMOTE_ADDR");
Request.ServerVariables("REMOTE_HOST");


But the hostname is largely meaningless across the Internet. This is a useful ref:

http://www.w3schools.com/asp/coll_servervariables.asp[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900