System.Web.HttpContext.Current.User.Identity.Name Vs System.Environment.UserName in ASP.NET
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.
Whats the Difference between System.Environment.Username and User.Identity.Name in C# and MVC?
System.Environment.Username
would give you the username of the currently logged in user running the process - it's the same as typing SET in a command prompt and seeing the %USERNAME% environment variable. In a web hosting environment, it would either be some application account running the IIS application pool. In some cases, like when you're inside a domain-enabled network and using Windows authentication for your website, and you've enabled identity impersonation, then System.Environment.Username
might give you the name of the user who is accessing your site, but that's only for specific scenarios.
User.Identity.Name
, however, gives you the name of the user who has authenticated in your website. If you're using Windows authentication, it will be the Windows username. If you're using Basic authentication, it'll be the username typed in the login box. If you're using any other authentication scheme, either standard or custom, that's using the ASP.NET Authentication framework, you'll get the logged-in username. This is why it's the recommended way to get the current logged in username - not System.Environment
, not System.Security.Principal.WindowsIdentity.GetCurrent
- just User.Identity
.
How does HttpContext.Current.User.Identity.Name know which usernames exist?
The HttpContext.Current.User.Identity.Name returns null
This depends on whether the authentication mode is set to Forms or Windows in your web.config file.
For example, if I write the authentication like this:
<authentication mode="Forms"/>
Then because the authentication mode="Forms", I will get null for the username. But if I change the authentication mode to Windows like this:
<authentication mode="Windows"/>
I can run the application again and check for the username, and I will get the username successfully.
For more information, see System.Web.HttpContext.Current.User.Identity.Name Vs System.Environment.UserName in ASP.NET.
Why does System.Web.HttpContext.Current.User.Identity.Name return an empty string?
It looks like you have an anonymous user. If you don't want to allow anonymous users, add the following to web.config:
<system.web>
<authorization>
<deny users="?" />
</authorization>
</system.web>
Cant get HttpContext.Current.User.Identity to return values
Have you wrote this in your web config ??
<authentication mode="Windows"/>
and please just take a look this
System.Web.HttpContext.Current.User.Identity.Name Vs System.Environment.UserName in ASP.NET
How do I get the current username in .NET using C#?
string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
Related Topics
How to Parse Very Huge Xml Files in C#
Ef Code First - How to Set Identity Seed
An Object Reference Is Required for the Non-Static Field, Method, or Property
Get List of Zero Reference Codes in Visual Studio
Open File Dialog and Select a File Using Wpf Controls and C#
When Is Finally Run If You Throw an Exception from the Catch Block
What Is the C# Equivalent of Nan or Isnumeric
Get Number of Listeners, Clients Connected to Signalr Hub
Double.Tryparse or Convert.Todouble - Which Is Faster and Safer
A Dependent Property in a Referentialconstraint Is Mapped to a Store-Generated Column
Measure a String Without Using a Graphics Object
Convert from Scientific Notation String to Float in C#
C#: How to Get an Object by the Name Stored in String
What Does Default(Object); Do in C#
Winforms: Application.Exit VS Environment.Exit VS Form.Close