簡體   English   中英

如何獲取當前登錄的用戶名

[英]How to Get the currently logged on user name

我正在構建一個小型 Intranet 應用程序,我需要在其中顯示打開網頁的人的用戶名。

我嘗試:

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

但這總是在網頁上顯示我的用戶名。

請參閱- 基本上您需要啟用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

PS:別忘了先檢查實體是否是null。

使用此代碼

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

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

請參閱http://forums.asp.net/t/1102996.aspx/1 我認為您看到自己名字的原因是 web 應用程序正在您的憑據下運行。

因此請改用HttpContext.Current.User.Identity.Name並確保您已在 web.config 中啟用Forms 身份驗證

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM