Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Web.HttpContext.Current.User.Identity.Name Vs System.Environment.UserName in ASP.NET

Tags:

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.

like image 828
FMFF Avatar asked Jan 12 '12 20:01

FMFF


People also ask

What is HttpContext current user identity Name?

It just holds the username of the user that is currently logged in. After login successful authentication, the username is automatically stored by login authentication system to "HttpContext.Current.User.Identity.Name" property.

Where is HttpContext user stored?

It is most likely stored in Managed Passwords: Click Start > Run. Enter "control userpasswords2"

Why is user identity Isauthenticated false?

isauthenticated is False when a user is already logged in.

How is HttpContext created?

When an HTTP request arrives at the server, the server processes the request and builds an HttpContext object. This object represents the request which your application code can use to create the response. The HttpContext object constructed by the ASP.NET Core web server acts as a container for a single request.


2 Answers

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

  • MSDN - HttpContext.User Property
  • MSDN - Environment.UserName Property
like image 112
dknaack Avatar answered Sep 21 '22 15:09

dknaack


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.

like image 20
kprobst Avatar answered Sep 18 '22 15:09

kprobst