Reputation: 87
I am working in MVC programming Environment I came Across the above doubt While working Authentication Process Of website?
Upvotes: 1
Views: 5098
Reputation: 11
Thanks to @Avner Shahar-Kashtan
More about Environment.UserName property
There is no one-size-fits-all approach it depends on the context. HttpContext is for use in ASP.Net applications. A console application and windows service are not an ASP.Net application and therefore you are not working in a web context.
The user name of the person who is associated with the current thread.
If an ASP.NET application runs in a development environment, the UserName property returns the name of the current user.
In a published ASP.NET application, this property returns the name of the application pool account (such as Default AppPool).
https://learn.microsoft.com/en-us/dotnet/api/system.environment.username?view=net-6.0
Asp.Net Core 3.1 Console App on VS for Mac
String currentUserName = System.Environment.UserName;
String currentUserDomainName = System.Environment.UserDomainName;
Console.WriteLine($"Current User: {currentUserDomainName}\\{currentUserName}");
Output: Mehmets-MacBook-Pro-2\baytas
Only Windows environment you can access this object. Returns a WindowsIdentity object that represents the current Windows user.
Dim currentUserName As String = System.Security.Principal.WindowsIdentity.GetCurrent().Name
Console.WriteLine("Current User: " & currentUserName)
Output: Domain\user
Upvotes: 0
Reputation: 4170
System.Environment.Username
its the windows user name who is currently logged in.
while
User.Identity.Name
its user name which is used to authenticate the site. If you using the windows
authentication then it will return the windows user name. But if you using the FormAuthentication
, it will return the currently logged in user name.
Upvotes: 1
Reputation: 14700
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
.
Upvotes: 3