How do I get the currently loggedin Windows account from an ASP.NET page?

StackTrace picture StackTrace · Apr 24, 2013 · Viewed 92.7k times · Source

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 the ASP.NET application, but into Windows) when data is edited in a page.

If I use Context.User.Identity.Name.Tostring(), I get the user name logged into the ASP.NET application, but I need the Windows account name.

System.Security.Principal.WindowsIdentity.GetCurrent().Name.Tostring()

Also, it only works when I run the website from Visual Studio, but after deploying to IIS it returns NT AUTHORITY\SYSTEM.

Answer

Zo Has picture Zo Has · Apr 24, 2013

You have to set authentication mode to Windows in your configuration & also disable anonymous users in authorization tag.