23

What is the difference between System.Web.HttpContext.Current.User.Identity.Name and System.Environment.UserName in the context of a ASP.Net Web Application Project?

Here's the code of what I'm trying to do:

Database myDB = DatabaseFactory.CreateDatabase();
bool IsAuthUser = myDB.ExecuteScalar("procIsAuthorizedUser", System.Environment.UserName);

If they are functionally identical, which is better in terms of performance?

This is a C# 4.0/ASP.Net web application which will see moderate usage internally in the organization. Thank you for the answers.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
FMFF
  • 1,652
  • 4
  • 32
  • 62

2 Answers2

40

Description

System.Web.HttpContext.Current.User.Identity.Name

Gets or sets security information for the current HTTP request. (The Name of the Logged in user on your Website)

System.Environment.UserName

Gets the user name of the person who is currently logged on to the Windows operating system.

More Information

Community
  • 1
  • 1
dknaack
  • 60,192
  • 27
  • 155
  • 202
17

System.Environment.UserName returns the identity under which the app pool that hosts your web app is running. If you're using Windows authentication and impersonation then it will be the actual user's name, however in all cases you're better off using the information provided by the HTTP context. There is no performance hit either way.

kprobst
  • 16,165
  • 5
  • 32
  • 53
  • 5
    I get username with the following: `username = HttpContext.Current.Request.LogonUserIdentity.Name.ToString().Split('\\')[1];` How can I get the display name of the user? – SearchForKnowledge Jun 25 '15 at 15:53