C# ASP.NET 中的 System.Web.HttpContext.Current.User.Identity.Name 与 System.Environment.UserName

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8841816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 04:45:10  来源:igfitidea点击:

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

c#asp.nethttpcontext

提问by FMFF

What is the difference between System.Web.HttpContext.Current.User.Identity.Nameand System.Environment.UserNamein the context of a ASP.Net Web Application Project?

ASP.Net Web 应用程序项目的上下文System.Web.HttpContext.Current.User.Identity.NameSystem.Environment.UserName上下文之间有什么区别?

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.

这是一个 C# 4.0/ASP.Net web 应用程序,它将在组织内部看到适度的使用。谢谢你的回答。

采纳答案by kprobst

System.Environment.UserNamereturns 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.

System.Environment.UserName返回承载您的 Web 应用程序的应用程序池正在运行的标识。如果您使用的是 Windows 身份验证和模拟,那么它将是实际用户的名称,但是在所有情况下,您最好使用 HTTP 上下文提供的信息。无论哪种方式,都不会影响性能。

回答by dknaack

Description

描述

System.Web.HttpContext.Current.User.Identity.Name

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)

获取或设置当前 HTTP 请求的安全信息。(您网站上登录用户的姓名)

System.Environment.UserName

系统.环境.用户名

Gets the user name of the person who is currently logged on to the Windows operating system.

获取当前登录到 Windows 操作系统的人员的用户名。

More Information

更多信息