Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 304
StackTrace Avatar asked Apr 24 '13 06:04

StackTrace


People also ask

How do I get the current username in .NET using C#?

GetCurrent(). Name; Returns: NetworkName\Username. Gets the user's Windows logon name.

How do I find my Windows authentication Username?

Click on "Task Manager." 4. In the new menu, select the "Users" tab. Your username will be listed here.

What gives domain username of the current user of the local machine?

Type whoami and press Enter. Your current user name will be displayed.

What is user identity name?

Techopedia Explains User Identification (User ID)Typically in an authentication process, user ID is used in conjunction with a password. The end-user must provide both of the credentials correctly to gain access to the system or application.


2 Answers

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

like image 100
Zo Has Avatar answered Oct 22 '22 00:10

Zo Has


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.

like image 30
Dawid Dziadkiewicz Avatar answered Oct 21 '22 23:10

Dawid Dziadkiewicz