我怎样才能获取客户端的主机名,本地计算机名和用户名在C#和asp.net [英] How I can obtain client hostname, local computer name and user name in c# and asp.net
问题描述
我的IP地址,但由于某些原因,我可以得到正确的名称解析,显示本地计算机名称。我尝试一些东西,他们都正显示出服务器主机名?
ipadd = HttpContext.Current.Request.ServerVariables [REMOTE_ADDR];
ip地址MYIP = IPAddress.Parse(ipadd);
IPHostEntry GetIPHost = Dns.GetHostEntry(MYIP);
// userhostname = System.Environment.MachineName;
// userhostname = Dns.GetHostName(); //解决ServerHostName没有电脑
// userhostname = HttpContext.Current.Request.UserHostName;
// userhostname = HttpContext.Current.Request.UserAgent;
userhostname = GetIPHost.HostName;
有关用户名,我特林使用
nametext = WindowsIdentity.GetCurrent()名称。
//服务器部署时,显示服务器名称
//当debuging和localY运行显示登录正确的当前用户// nametext = HttpContext.Current.Request.ServerVariables [LOGON_USER];
//不返回任何东西
我交换身份验证和没有结果。
<身份验证模式=表格>
<身份验证模式=窗口>
使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.htt$p$pquest.userhostaddress.aspx\"><$c$c>Htt$p$pquest.UserHostAddress$c$c>和<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.htt$p$pquest.userhostname.aspx\"><$c$c>Htt$p$pquest.UserHostName$c$c>客户端IP和机器名。
假设你有身份验证正确配置,你可以从<一个获取客户端用户href=\"http://msdn.microsoft.com/en-us/library/system.security.principal.iidentity.aspx\"><$c$c>IIdentity.Name$c$c>.
在一个 页面的上下文
,则可以使用 Request.UserHostAddress
, Request.UserHostName
和 User.Identity.Name
。
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
.
这篇关于我怎样才能获取客户端的主机名,本地计算机名和用户名在C#和asp.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!