5

I have the IP address, but for some reason I can get the name resolve correctly to show local computer name. I try few things and all of them is showing the server hostname?

    ipadd = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
    IPAddress myIP = IPAddress.Parse(ipadd);
    IPHostEntry GetIPHost = Dns.GetHostEntry(myIP);


    //userhostname = System.Environment.MachineName;
    //userhostname = Dns.GetHostName(); //Resolve ServerHostName not computer
    //userhostname = HttpContext.Current.Request.UserHostName;
    //userhostname = HttpContext.Current.Request.UserAgent;
    userhostname = GetIPHost.HostName;

For user name I am tring to use

nametext = WindowsIdentity.GetCurrent().Name;
//Shows server name when deployed on server 
//when debuging and running localy shows correctly current user logged in

//nametext = HttpContext.Current.Request.ServerVariables["LOGON_USER"]; 
// not returning anything

I switched authentication and no results

<authentication mode="Forms">
<authentication mode="Windows">
bluevector
  • 3,485
  • 1
  • 15
  • 18
laspalmos
  • 149
  • 1
  • 3
  • 20
  • Is this running on the internet or on an intranet? If it's running on the web, you might not be able to access details such as hostname, and windows username for security reasons. I've never tried, but it seems like those things might be protected/unavailable. – Localghost Jul 10 '12 at 18:19
  • This is on intranet. I think the problem is at authentication mode on IIS i have right now Anonymous authentication enabled in IIS 7. I was thinking to use JavaScript function to retrieve thise information. – laspalmos Jul 10 '12 at 18:29
  • You will need to turn on windows authentication for this to work I believe. – Localghost Jul 10 '12 at 18:46
  • I would prefer with annonymous auth. It looks like it won't be passible. Thanks – laspalmos Jul 10 '12 at 19:23

2 Answers2

7

Use HttpRequest.UserHostAddress and HttpRequest.UserHostName for client IP and machine name.

Assuming you have authentication configured correctly, you can get the client user from IIdentity.Name.

In the context of a Page, you can use Request.UserHostAddress, Request.UserHostName and User.Identity.Name.

jrummell
  • 42,637
  • 17
  • 112
  • 171
  • I try use what you suggested but I recieved exactly same data as from my examples. I forgot to look at annonymous authentication mode, but still I would like to see it without changing it if possible. – laspalmos Jul 10 '12 at 18:32
  • 2
    You can't get the username with anonymous auth as there is no user. – jrummell Jul 10 '12 at 18:34
  • What about when I try in debug mode in VS it shows user with domain? It's because VS IIS server has annonymous authentication disabled? Do you happend to konw if I can do it in JavaScript? Thanks for your help. – laspalmos Jul 10 '12 at 18:46
  • See [IIS Express Windows Authentication](http://stackoverflow.com/a/7168308/26226) for VS and windows authentication. – jrummell Jul 10 '12 at 18:57
  • 2
    No, I'm not aware of a way to get the username from javascript (without ajax that uses server side code to access `User.Identity.Name`). See [Get username of logged in user in os via browser](http://stackoverflow.com/questions/5271742/get-username-of-logged-in-user-in-os-via-browser). – jrummell Jul 10 '12 at 18:59
  • This is explanation what I was looking and uanble to find. Thanks – laspalmos Jul 10 '12 at 19:21
0

I use this to show the client locale machine on my intranet (using System.Net) :

Dns.GetHostEntry(Request.UserHostAddress).HostName.ToString()
yonel
  • 21
  • 3