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

吃可爱长大的小学妹 提交于 2019-11-26 07:46:33

问题


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.


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/8841816/system-web-httpcontext-current-user-identity-name-vs-system-environment-username

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!