2011-07-07 172 views
2

我正在构建一个小型Intranet应用程序,其中需要显示打开网页的用户的用户名。如何获取当前登录的用户名

我尝试:

WindowsIdentity.GetCurrent().Name.ToString(); 

但这始终显示在网页上我的用户名。

+1

你是否与另一位用户尝试过? – ChrisBint

+0

是的,我尝试从另一个用户 – Simsons

+0

AFAIK这只适用于模拟打开并正确设置。 。WindowsIdentity.GetCurrent()Name.ToString();可以显示'启动'虚拟主机的用户名,而不是访问该网站的用户。也许一些基于mac地址/ ipadres /机器名称的方案会更方便,并且可以与非Iexplore浏览器一起使用。 – CodingBarfield

回答

5

请参阅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的字符串。

0

您可以使用此:

Page.User.Identity.Name 

P.S:不要忘记检查的实体是否是空第一。

+0

或在MVC:HttpContext.Current.User.Identity.Name – jao

0

使用此代码

if (User.Identity.IsAuthenticated) 
Label1.Text = User.Identity.Name; 
else 
Label1.Text = "No user identity available."; 

看看下面的文章Forms Authentication in ASP.NET

+0

总是得到没有用户身份可用 – Simsons

+0

您使用什么类型的身份验证? – Serghei

+0

然后你有一个破损的登录系统。你使用'FormsAuthentication'吗? –

相关问题