我正在构建一个小型Intranet应用程序,其中需要显示打开网页的用户的用户名。如何获取当前登录的用户名
我尝试:
WindowsIdentity.GetCurrent().Name.ToString();
但这始终显示在网页上我的用户名。
我正在构建一个小型Intranet应用程序,其中需要显示打开网页的用户的用户名。如何获取当前登录的用户名
我尝试:
WindowsIdentity.GetCurrent().Name.ToString();
但这始终显示在网页上我的用户名。
请参阅THIS - basicaly需要启用Integrated windows authentication
和使用下列之一:
System.Security.Principal.WindowsPrincipal p = System.Threading.Thread.CurrentPrincipal as System.Security.Principal.WindowsPrincipal;
string strName = p.Identity.Name;
或
string strName = HttpContext.Current.User.Identity.Name.ToString();
或
string strName = Request.ServerVariables["AUTH_USER"]; //Finding with name
string strName = Request.ServerVariables[5]; //Finding with index
所有3种情况都应该重试包含DomainName\WinNTLoggedUserName
的字符串。
您可以使用此:
Page.User.Identity.Name
P.S:不要忘记检查的实体是否是空第一。
或在MVC:HttpContext.Current.User.Identity.Name – jao
使用此代码
if (User.Identity.IsAuthenticated)
Label1.Text = User.Identity.Name;
else
Label1.Text = "No user identity available.";
见http://forums.asp.net/t/1102996.aspx/1。我认为你看到自己的名字的原因是,Web应用程序正在你的凭据下运行。
所以使用HttpContext.Current.User.Identity.Name
代替,并确保您已在web.config中启用Forms authentication
你是否与另一位用户尝试过? – ChrisBint
是的,我尝试从另一个用户 – Simsons
AFAIK这只适用于模拟打开并正确设置。 。WindowsIdentity.GetCurrent()Name.ToString();可以显示'启动'虚拟主机的用户名,而不是访问该网站的用户。也许一些基于mac地址/ ipadres /机器名称的方案会更方便,并且可以与非Iexplore浏览器一起使用。 – CodingBarfield