2015-05-05 35 views
0

我想要用戶登錄系統的那個用戶名的用戶名。如何找到登錄系統的用戶名

System.Security.Principal.WindowsPrincipal p = System.Threading.Thread.CurrentPrincipal as System.Security.Principal.WindowsPrincipal; 
string strName = p.Identity.Name; 

我已經寫了上面的代碼,但沒有得到該用戶名,但它給別名

+0

你能不能舉個例子? (我假設你登錄到AD。) –

+0

看這裏:http://stackoverflow.com/questions/5218778/how-to-get-currently-logged-username-from-windows-service-in-net – DasDas

+0

我我正在做這個,然後我得到別名(例如[email protected]),但我想這個帳戶的用戶名(用戶名:Ramandeep辛格) –

回答

2
var username = Environment.UserName 
1

string strName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;