asp.net怎样获取客户端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用户名?
2和4虽然能够实现,但是不够好,因为会弹出登陆验证,即使能够做到不弹验证,他们也是采用Coolie方式,所以获取的用户名并不一定准确。
JS方法如下:
var WshNetwork = new ActiveXObject("WScript.Network");
alert("Domain_Name =" + WshNetwork.UserDomain);
alert("Computer_Name =" + WshNetwork.ComputerName);
alert("User_Name =" + WshNetwork.UserName);
獲取JS有什麽方法?上面的要設置IE安全性才行,所以不是很好