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

前端 未结 8 916
余生分开走
余生分开走 2020-12-01 12:25

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

相关标签:
8条回答
  • 2020-12-01 13:02

    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.

    0 讨论(0)
  • 2020-12-01 13:06

    To get the currently logged-in user to Windows in C#, use:

    string Username = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
    
    0 讨论(0)
提交回复
热议问题