System.Web.Httpcontext.Current.User.Identity.Name VS System.Environment.Username in ASP.NET

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



Leave a reply



Submit