简体   繁体   中英

How I can obtain client hostname, local computer name and user name in c# and asp.net

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">

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 .

我使用它来显示我的Intranet上的客户端语言环境机器(使用System.Net):

Dns.GetHostEntry(Request.UserHostAddress).HostName.ToString()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM