简体   繁体   English

C# 和 MVC 中的 System.Environment.Username 和 User.Identity.Name 有什么区别?

[英]Whats the Difference between System.Environment.Username and User.Identity.Name in C# and MVC?

I am working in MVC programming Environment I came Across the above doubt While working Authentication Process Of website?我在 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. System.Environment.Username将为您提供运行该进程的当前登录用户的用户名-与在命令提示符下键入SET并看到%USERNAME%环境变量相同。 In a web hosting environment, it would either be some application account running the IIS application pool. 在虚拟主机环境中,它可能是某个运行IIS应用程序池的应用程序帐户。 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. 在某些情况下,例如当您位于启用了域的网络中并且对网站使用Windows身份验证并且启用了身份模拟时, System.Environment.Username 可能会为您提供访问站点的用户名,但这仅适用于特定情况。

User.Identity.Name , however, gives you the name of the user who has authenticated in your website. 但是, User.Identity.Name会为您提供在您的网站中进行身份验证的用户的名称。 If you're using Windows authentication, it will be the Windows username. 如果您使用Windows身份验证,它将是Windows用户名。 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. 如果您使用的是标准身份验证或自定义身份验证的其他身份验证方案,则使用ASP.NET身份验证框架,您将获得登录的用户名。 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 . 这就是为什么它是获取当前登录用户名的推荐方法的原因-不是System.Environment ,不是System.Security.Principal.WindowsIdentity.GetCurrent仅是User.Identity

System.Environment.Username

its the windows user name who is currently logged in. 当前登录的Windows用户名。

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. 如果您使用windows身份验证,则它将返回Windows用户名。 But if you using the FormAuthentication , it will return the currently logged in user name. 但是,如果使用FormAuthentication ,它将返回当前登录的用户名。

Thanks to @Avner Shahar-Kashtan感谢@Avner Shahar-Kashtan

More about Environment.UserName property有关Environment.UserName属性的更多信息

There is no one-size-fits-all approach it depends on the context.没有一种万能的方法,它取决于上下文。 HttpContext is for use in ASP.Net applications. HttpContext 用于 ASP.Net 应用程序。 A console application and windows service are not an ASP.Net application and therefore you are not working in a web context.控制台应用程序和 windows 服务不是 ASP.Net 应用程序,因此您不在 web 上下文中工作。

The user name of the person who is associated with the current thread.与当前线程关联的人员的用户名。

  • On Windows the UserName property wraps a call to the Windows GetUserName function.在 Windows 上,UserName 属性包装了对 Windows GetUserName function 的调用。
  • On Unix based platforms (Linux, Mac etc.) the UserName property wraps a call to the在基于 Unix 的平台(Linux、Mac 等)上,UserName 属性包含对
    getpwuid_r function. getpwuid_r function。

If an ASP.NET application runs in a development environment, the UserName property returns the name of the current user.如果 ASP.NET 应用程序在开发环境中运行,则 UserName 属性返回当前用户的名称。

In a published ASP.NET application, this property returns the name of the application pool account (such as Default AppPool).在已发布的 ASP.NET 应用程序中,此属性返回应用程序池帐户的名称(例如 Default AppPool)。

在此处输入图像描述

https://learn.microsoft.com/en-us/do.net/api/system.environment.username?view.net-6.0 https://learn.microsoft.com/en-us/do.net/api/system.environment.username?view.net-6.0

Asp.Net Core 3.1 Console App on VS for Mac VS for Mac 上的 Asp.Net Core 3.1 控制台应用程序

String currentUserName = System.Environment.UserName;
String currentUserDomainName = System.Environment.UserDomainName;
Console.WriteLine($"Current User: {currentUserDomainName}\\{currentUserName}");

Output: Mehmets-MacBook-Pro-2\baytas Output:Mehmets-MacBook-Pro-2\baytas

Only Windows environment you can access this object. Returns a WindowsIdentity object that represents the current Windows user.只有 Windows 环境你可以访问这个 object。返回一个代表当前 Windows 用户的 WindowsIdentity object。

Dim currentUserName As String = System.Security.Principal.WindowsIdentity.GetCurrent().Name
Console.WriteLine("Current User: " & currentUserName)

Output: Domain\user Output:域\用户

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM