2012-07-10 113 views
3

我有IP地址,但出於某種原因,我可以正確解析名稱以顯示本地計算機名稱。我嘗試了幾件事,他們都顯示服務器主機名?如何在c#和asp.net中獲取客戶端主機名,本地計算機名稱和用戶名

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; 

對於用戶名,我特林使用

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 

我交換身份驗證和沒有結果

<authentication mode="Forms"> 
<authentication mode="Windows"> 
+0

是互聯網上或Intranet上的這個運行?如果它在網絡上運行,出於安​​全原因,您可能無法訪問諸如主機名和Windows用戶名等詳細信息。我從來沒有嘗試過,但似乎這些事情可能會受到保護/不可用。 – Jim 2012-07-10 18:19:51

+0

這是在內部網上。我認爲問題是在IIS上的身份驗證模式中,我現在已經在IIS 7中啓用匿名身份驗證。我正在考慮使用JavaScript函數來檢索此信息。 – laspalmos 2012-07-10 18:29:27

+0

您需要打開Windows身份驗證才能正常工作,我相信。 – Jim 2012-07-10 18:46:44

回答

6

使用HttpRequest.UserHostAddressHttpRequest.UserHostName客戶端IP和機器名。

假設您的身份驗證配置正確,您可以從IIdentity.Name獲取客戶端用戶。

Page的背景下,您可以使用Request.UserHostAddress,Request.UserHostNameUser.Identity.Name

+0

我嘗試使用你的建議,但我收到了與我的例子完全相同的數據。我忘了查看匿名身份驗證模式,但仍然希望在不改變它的情況下查看它。 – laspalmos 2012-07-10 18:32:32

+2

由於沒有用戶,您無法獲得匿名認證的用戶名。 – jrummell 2012-07-10 18:34:00

+0

當我在VS的調試模式下嘗試顯示帶有域的用戶時呢?這是因爲VS IIS服務器已禁用了匿名身份驗證?如果我能用JavaScript做到這一點,你有沒有想過要知道?謝謝你的幫助。 – laspalmos 2012-07-10 18:46:26

0

我用它來顯示客戶端區域的機器上我的企業內部網(使用System.Net):

Dns.GetHostEntry(Request.UserHostAddress).HostName.ToString() 
相關問題