简体   繁体   English

如何从UserPrincipal获取帐户名?

[英]How do I get the account name from UserPrincipal?

I am using Active Directory login in one of my Winforms applications. 我在一个Winforms应用程序中使用Active Directory登录。 I get a UserPrincipal object but I'm not sure how to get the proper accountname from this object? 我有一个UserPrincipal对象,但不确定如何从该对象中获取正确的帐户名? I can see that there is a SamAccountName that is the same as my account name but it states that this is for old versions of Windows. 我可以看到有一个SamAccountName与我的帐户名相同,但是它指出这是用于Windows的旧版本。 So how do I get the proper account name no mather old or new Windows systems? 那么,如何获得正确的帐户名而不用旧的或新的Windows系统?

This is the code I use to get the object 这是我用来获取对象的代码

if (ADUserName.Length > 0)
    context = new PrincipalContext(ContextType.Domain, ADServer, null,  ADUserName, ADUserPassword);
else
    context = new PrincipalContext(ContextType.Domain, ADServer);

UserPrincipal.FindByIdentity(context, account)

SamAccountName is the "account name" as it was used in Windows even before Active Directory was introduced. SamAccountName 甚至引入Active Directory 之前在Windows中使用的“帐户名”。

It's "legacy" in that it has been supplemented by additional info - your "distinguished name" in the LDAP context etc. - but it is the Windows account name ("short name") - and it's still a mandatory field for any new user being created - just use it! 它是“旧版”,已通过附加信息进行了补充-您在LDAP上下文中的“专有名称”等-但它 Windows帐户名(“简称”),并且对于任何新用户来说,它仍然是必填字段被创建-只需使用它!

See User naming attributes for more details on which name is used for what in an AD context. 有关在AD上下文中使用哪个名称的详细信息,请参阅用户命名属性

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

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