[英]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.";
請參閱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.