日期:2014-05-17  浏览次数:20942 次

怎样获取客户端Windows用户名?
我的项目发布在服务器,想做一个功能,当客户端访问网站时可以获取到这个客户端的Windows登陆用户名,
在网上找了很多方法都不行,如下:
1. System.Security.Principal.WindowsPrincipal p = System.Threading.Thread.CurrentPrincipal as System.Security.Principal.WindowsPrincipal;
           string strName = p.Identity.Name;
2.string strName = HttpContext.Current.User.Identity.Name;
这两种去不到值,Web.config也配置了如下:
<authentication mode="Windows"/>
<identity impersonate="true"/>
3.string userName = Environment.UserName;
4.WindowsIdentity identity = WindowsIdentity.GetCurrent();
  string userName = identity.Name;
  3和4只是获取本地用户(服务器)
5.Page.Request.UserHostAddress;
  Page.Request.UserHostName
这两种只是获取客户端IP地址

谁知道怎样获取客户端Windows用户名?

------解决方案--------------------
用Principal没问题。我觉得是要在Web服务器上启用Windows认证才能得到客户端Windows用户名。
------解决方案--------------------
通常这是不行也是没必要的,别人是通过浏览器去访问的,浏览器可以获取,但是你的程序是在网页里运行的,浏览器的内部信息你是没权利获取的,要不然就是中毒了,杀软会狂叫的
可以自己写个ActiveX让客户都装上去,如果有必要
------解决方案--------------------
第四款
------解决方案--------------------
System.Net.IPAddress ip = System.Net.IPAddress.Parse(Page.Request.UserHostAddress);
System.Net.IPHostEntry ine = System.Net.Dns.GetHostByAddress(ip);
Page.Response.Write(ine.HostName);
------解决方案--------------------
引用:
System.Net.IPAddress ip = System.Net.IPAddress.Parse(Page.Request.UserHostAddress);
System.Net.IPHostEntry ine = System.Net.Dns.GetHostByAddress(ip);
Page.Response.Write(ine.HostName);

你是localhost吧,呵呵,你这要求能穿透客户端的路由器