0

whats the difference between Environment.UserName; and System.Security.Principal.WindowsIdentity.GetCurrent().Name; and HttpContext.Current.User ?

As per my understanding HttpContext.Current.User returns Logged in user , System.Security.Principal.WindowsIdentity.GetCurrent().Name returns the identuty under which code is running. But What is the use of Environment.UserName; ?

Vedda
  • 7,066
  • 6
  • 42
  • 77
Vishwas Rao
  • 121
  • 1
  • 1
  • 11
  • Possible duplicate of [What's the difference between HttpContext.Current.User and Thread.CurrentPrincipal in asp.net?](https://stackoverflow.com/questions/1843271/whats-the-difference-between-httpcontext-current-user-and-thread-currentprincip) – Lex Li Apr 20 '18 at 00:13

1 Answers1

2

Environment.UserName returns just the user name string of the logged into the Windows OS user - i.e. 'JohnDoe'

System.Security.Principal.WindowsIdentity.GetCurrent().Name returns NetBIOS user name of the logged into the Windows OS user - i.e. 'Fabrikam\JohnDoe'

HttpContext.Current.User returns the user object of the logged into the website user, given the website is leveraging the membership system. To get the username string you need to navigate through the Identity property:

string userName = HttpContext.Current.User.Identity.Name

Bozhidar Stoyneff
  • 3,576
  • 1
  • 18
  • 28