C# 如何以 username@domain 格式获取当前 Windows 用户名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12166589/
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
How do I get the current windows user's name in username@domain format?
提问by Tony Vitabile
I know that the following function returns the current Windows user's name in domain\username format.
我知道以下函数以域\用户名格式返回当前的 Windows 用户名。
Convert.ToString( WindowsIdentity.GetCurrent().Name );
But how do I obtain the user's name in username@domainformat?
但是如何以username@domain格式获取用户名呢?
EDIT:
编辑:
I'm responding in this edit as everyone who has replied has the same basic idea.
我在此编辑中做出回应,因为每个回复的人都有相同的基本想法。
From what I've been given to understand, parsing the name from domain\usernameformat and constructing it as username@domainis not safe or advised. I believe this is so because there is no guarantee that the two domain names are the same in the different formats. For example, in the company where I work, the domainpart of the domain\usernameformat is based upon deparment, but in the username@domain, it's the company name. It's the kind of thing that requires a DNS lookup.
据我所知,从domain\username格式解析名称并将其构造为username@domain不安全或不建议。我相信这是因为不能保证两个域名在不同格式下是相同的。例如,在我工作的公司中,格式的domain一部分domain\username是基于 deparment 的,但在 中username@domain,它是公司名称。这是一种需要 DNS 查找的东西。
I was hoping that there was an API that did this DNS lookup. I guess I should have put this information into my original question. Sorry.
我希望有一个 API 可以进行 DNS 查找。我想我应该把这些信息放到我原来的问题中。对不起。
采纳答案by Tony Vitabile
All of the code to take the name in Domain\user nameformat and parse it will not work in all situations. The answer is you have to make calls to Active Directory to get the User Principal Name. It turns out that I can't rely on Active Directory being installed on the desktop, since many police departments don't install the directory on their laptops in case it is stolen while a cop is not in the car. (Talk about gutsy, stealing a computer out of a police vehicle!)
以Domain\user name格式获取名称并解析它的所有代码在所有情况下都不起作用。答案是您必须调用 Active Directory 以获取用户主体名称。事实证明,我不能依赖安装在桌面上的 Active Directory,因为许多警察部门不会在他们的笔记本电脑上安装该目录,以防在警察不在车内时它被盗。(胆大包天,从警车里偷电脑!)
We have come up with our own solution for our situation. We store the user names in our database in Domain\user nameformat. When the program starts up, it checks to see if the current windows user name (in the same format) is in the database. If it is, the program uses that user as the current user and runs. If the current Windows user is not in our database, the program falls back to our previous code.
我们已经针对我们的情况提出了自己的解决方案。我们将用户名以Domain\user name格式存储在我们的数据库中。当程序启动时,它会检查当前的 Windows 用户名(格式相同)是否在数据库中。如果是,程序将使用该用户作为当前用户并运行。如果当前的 Windows 用户不在我们的数据库中,程序将回退到我们之前的代码。
This way, the user can log into the machine using any format for their user name and they authenticate with Windows. Our program always gets the user name in the same format and it always checks the user name in that format. Windows authenticates the user and not us.
通过这种方式,用户可以使用任何格式的用户名登录计算机,并通过 Windows 进行身份验证。我们的程序总是以相同的格式获取用户名,并且它总是以该格式检查用户名。Windows 验证用户而不是我们。
回答by StoriKnow
You could split the name using \as the delimiter, then reverse the order like so:
您可以使用\分隔符分割名称,然后像这样颠倒顺序:
string[] splitName = WindowsIdentity.GetCurrent().Name.Split('\');
//check that splitName contains at least 2 values before using
string name = (splitName.Length > 1) ? splitName[1] + "@" + splitName[0] : null;
It's important to note that a double backslash \\is required because a backslash is a special character. We add the second backslash in the above example to escape the special character and use it as a regular character.
需要注意的是双反斜杠\\是必需的,因为反斜杠是一个特殊字符。我们在上面的例子中添加了第二个反斜杠来转义特殊字符并将其用作常规字符。
回答by Aghilas Yakoub
var input = WindowsIdentity.GetCurrent().Name ;
string[] tab = input.Split('\');
var result = tab[1] + "@" + tab[0];
回答by Kevin
Something like this should work...
像这样的东西应该工作......
string[] temp = Convert.ToString(WindowsIdentity.GetCurrent().Name).Split('\');
string userName = temp[1] + "@" + temp[0];
回答by Evangelos Skianis
Something along these lines.
沿着这些路线的东西。
var nameParts = WindowsIdentity.GetCurrent().Name.Split(@"\");
string name = nameParts.Length == 1
? nameParts
: string.format("{0}@{1}",nameParts[1],nameParts[0]);
回答by Simon Giles
Use
用
System.DirectoryServices.AccountManagement.UserPrincipal.Current.UserPrincipalName
This returns the UPN of the current user. Requires a reference to System.DirectoryServices.AccountManagement.
这将返回当前用户的 UPN。需要对 System.DirectoryServices.AccountManagement 的引用。

