I have an ASP.NET 3.5 application that uses ASP.NET forms authentication. I want to be able to get the Windows user name currently logged into the computer (NOT logged into
To get the currently logged in user to a Windows account you have to use Windows authentication
instead of Forms authentication
:
System.Security.Principal.WindowsIdentity.GetCurrent().Name.Tostring() also only works when i run the website from visual studio but after deploying to IIS it returns NT AUTHORITY\SYSTEM
It shows the application current user. When you host your application on the Visual Studio web server it uses your local account. However, when you will log in to the web application with different credentials it will always show your current Windows login.
An application deployed to IIS uses the NT AUTHORITY\SYSTEM account in your case.
To get the currently logged-in user to Windows in C#, use:
string Username = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();