简体   繁体   English

"如何获取用户名@域格式的当前 Windows 用户名?"

[英]How do I get the current windows user's name in username@domain format?

I know that the following function returns the current Windows user's name in domain\\username format.我知道以下函数以域\\用户名格式返回当前 Windows 用户名。

Convert.ToString( WindowsIdentity.GetCurrent().Name );

Something like this should work... 像这样的东西应该工作......

string[] temp = Convert.ToString(WindowsIdentity.GetCurrent().Name).Split('\\');
string userName = temp[1] + "@" + temp[0];

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. 我们在上面的例子中添加第二个反斜杠来转义特殊字符并将其用作常规字符。

var input =  WindowsIdentity.GetCurrent().Name ;
string[] tab = input.Split('\\');
var result = tab[1] + "@" +  tab[0];

All of the code to take the name in Domain\\user name format and parse it will not work in all situations. Domain\\user name格式获取Domain\\user name并解析它的所有代码在所有情况下都不起作用。 The answer is you have to make calls to Active Directory to get the User Principal Name. 答案是您必须调用Active Directory才能获取用户主体名称。 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. 事实证明,我不能依赖桌面上安装的Active Directory,因为许多警察部门不会在他们的笔记本电脑上安装目录,以防警察在警察不在车内时被盗。 (Talk about gutsy, stealing a computer out of a police vehicle!) (谈论勇敢,从警车偷走电脑!)

We have come up with our own solution for our situation. 我们已针对我们的情况提出了自己的解决方案。 We store the user names in our database in Domain\\user name format. 我们以Domain\\user name格式将用户名存储在我们的数据库中。 When the program starts up, it checks to see if the current windows user name (in the same format) is in the database. 程序启动时,它会检查当前的Windows用户名(格式是否相同)是否在数据库中。 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. 如果当前Windows用户不在我们的数据库中,则该程序将回退到我们之前的代码。

This way, the user can log into the machine using any format for their user name and they authenticate with Windows. 这样,用户可以使用任何格式的用户名登录计算机,并通过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对用户进行身份验证,而不是我们。

Use 使用

System.DirectoryServices.AccountManagement.UserPrincipal.Current.UserPrincipalName

This returns the UPN of the current user. 这将返回当前用户的UPN。 Requires a reference to System.DirectoryServices.AccountManagement. 需要对System.DirectoryServices.AccountManagement的引用。

Something along these lines. 沿着这些方向的东西。

var nameParts = WindowsIdentity.GetCurrent().Name.Split(@"\");
string name = nameParts.Length == 1 
    ? nameParts  
    : string.format("{0}@{1}",nameParts[1],nameParts[0]);

I've seen so many variations of the same, why not put it into a function like:我已经看到了很多相同的变体,为什么不把它放到一个函数中,比如:

string GetUserName()
{
    var currentName =  WindowsIdentity.GetCurrent()?.Name;
    if (string.IsNullOrEmpty(currentName)) return null;
    var nameSplit = currentName.Split(@"\");
    string name = (nameSplit.Length > 1)
                    ? $"{nameSplit[1]}@{nameSplit[0]}" : currentName;
    return name;
}

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

相关问题 如何获取当前Windows登录用户的用户名? - How do I get the current Windows logged-in user's username? 如何获取当前用户的用户名,密码和域 - How to get username, password and domain of current user 在IIS Web应用程序中,如何获取Windows用户名? -不是进程用户名 - in an IIS web app, how do I get the windows user name? - not the process username 如何获取Windows 8应用程序的本地URL的生成域名? - How do I get the generated domain name for local URL's for a Windows 8 app? 如何在IIS 10上使用C#Razor获取Windows用户域/用户名? - How do you get Windows user domain/username with C# Razor on IIS 10? 如何在Windows 10中获取计算机域名 - How do I get the computer domain name in Windows 10 如何通过ldap中的域名获取用户的用户名和SID - How to get username and SID for user by a domain name in ldap 如何让当前用户登录Windows? - How do I get current user logged in to windows? 如何在 C# 控制台应用程序中获取当前 Windows 用户名? - How do I get Current Windows Username in C# Console Application? C# - Windows身份验证 - 如何从HttpContext用户获取Full doMain名称(FQDN) - C# - Windows authentication - How to get from HttpContext user's Full doMain name (FQDN)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM