2014-09-01 87 views
1

我想獲取我的網站託管在iis服務器上時的計算機名稱。需要獲得計算機名稱當在IIS上承載stite

當我從本地運行我的網站時,我將獲得用戶名。這是我的代碼。

PrincipalContext ctx = new PrincipalContext(ContextType.Domain); 
UserPrincipal userp = UserPrincipal.Current; 

但是當我發表我的網站IIS服務器上的I M沒有得到系統的名字

回答

0

您可以使用任何下面的代碼片段的。

System.Environment.MachineName 
HttpContext.Current.Server.MachineName 
System.Net.Dns.GetHostName() 
System.Net.Dns.GetHostEntry(Environment.MachineName).HostName 
Dns.GetHostByName("LocalHost").HostName 

希望這有助於

+0

我已經tryed這更早,但它不工作 – user3462829 2014-09-01 10:31:22

+0

我已經更新了我的答案。 – Shravan 2014-09-01 10:34:09

+0

我只在本地工作,當我上傳我的網站在網絡服務器上它nt工作 – user3462829 2014-09-01 10:51:05

0

試試這個:

添加這個命名空間:using System.Net

;

然後使用此:

protected void Page_Load(object sender, EventArgs e) 
    { 
     string IP = Request.UserHostName; 
     string compName = DetermineCompName(IP); 
     Response.Write(compName); 
} 

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(); 
    } 
+0

它也不工作檢查此鏈接:http://mobileapi.jobseeders.com/index.aspx 按第二個按鈕 – user3462829 2014-09-01 11:00:40

+0

設置斷點使用調試程序,然後檢查並告訴我'myIP'的值 – 2014-09-01 11:04:49

+0

myIP值= :: 1 – user3462829 2014-09-01 11:09:28

相關問題