windows 如何获取用户的网络登录名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/926920/
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-09-15 12:34:29  来源:igfitidea点击:

How can I get the users network login name?

c#windowsnetworking

提问by Malfist

I'm building a C# application, and I want to identify users by their username. For example, if I logged onto the domain mydomain as the user myusername I'd want to get the mydomain\myusername so I can identify the user.

我正在构建一个 C# 应用程序,我想通过用户名来识别用户。例如,如果我以用户 myusername 的身份登录域 mydomain,我想获取 mydomain\myusername 以便识别用户。

How can I do this with C#?

我怎样才能用 C# 做到这一点?

回答by Noldorin

It's very simple indeed:

确实很简单:

Environment.UserName

From the MSDN page:

MSDN 页面

The UserName property provides part of the credentials associated with computer domain names, as returned by the UserDomainName property. This information is typically represented as DOMAIN\username.

This property can be used to identify the current user to the system and application for security or access purposes. It can also be used to customize a particular application for each user.

UserName 属性提供与计算机域名关联的部分凭据,如 UserDomainName 属性返回的那样。此信息通常表示为 DOMAIN\username。

出于安全或访问目的,此属性可用于向系统和应用程序标识当前用户。它还可以用于为每个用户定制特定的应用程序。

You can also get the domain name using Environment.UserDomainName.

您还可以使用Environment.UserDomainName.

回答by jinsungy

try User.Identity.Nameor Environment.UserName.

尝试User.Identity.NameEnvironment.UserName

EDIT:

编辑:

Environment.UserDomainName - Domain

Environment.UserDomainName - 域

Environment.UserName - UserName

Environment.UserName - 用户名

回答by Timothy Carter

Environment.UserDomainNamewill get the domainand Environment.UserNamewill get the username.

Environment.UserDomainName将得到Environment.UserName将得到的用户名