从 UserPrincipal 对象获取 nETBIOSName
我正在使用 .Net 库的 System.DirectoryServices.AccountManagement 部分来连接到 ActiveDirectory。
在 GroupPrincipal 对象上调用 GetMembers() 并筛选结果后,我现在有了 UserPrincipal 对象的集合。
GroupPrincipal myGroup; // population of this object omitted here
foreach (UserPrincipal user in myGroup.GetMembers(false).OfType<UserPrincipal>())
{
Console.WriteLine(user.SamAccountName);
}
上面的代码示例将打印出诸如“TestUser1”之类的用户名。我需要将它们与来自另一个应用程序的“DOMAIN\TestUser1”格式的列表进行比较。
如何从 UserPrincipal 对象中获取“DOMAIN”部分?
我不能只附加已知域名,因为涉及多个域,并且我需要区分 DOMAIN1\TestUser1 和 DOMAIN2\TestUser2。
I am using the System.DirectoryServices.AccountManagement part of the .Net library to interface into ActiveDirectory.
Having called GetMembers() on a GroupPrincipal object and filter the results, I now have a collection of UserPrincipal objects
GroupPrincipal myGroup; // population of this object omitted here
foreach (UserPrincipal user in myGroup.GetMembers(false).OfType<UserPrincipal>())
{
Console.WriteLine(user.SamAccountName);
}
The above code sample will print out usernames like "TestUser1". I need to compare these to a list coming from another application in "DOMAIN\TestUser1" format.
How do I get the "DOMAIN" part from the UserPrincipal object?
I can't just append a known domain name as there are multiple domains involved and I need to differentiate DOMAIN1\TestUser1 and DOMAIN2\TestUser2.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我能想到的你有两个选择。
[email&nb] 右侧的所有内容sp;受保护]< /a>
;我不了解UserPrincipal,也不了解GroupPrincipal。另一方面,我知道有一种可行的方法可以实现你想要的目标。
此 SO 问题中提供的其他相关信息或链接。
C# Active Directory:获取用户的域名?
如何查找域的 NetBIOS 名称
You have two choices that I can think of.
[email protected]
;System.DirectoryServices
namespace.I don't know about UserPrincipal, neither do I about GroupPrincipal. On the other hand, I know of a working way to achive to what you want.
Other related information or links available in this SO question.
C# Active Directory: Get domain name of user?
How to find the NetBIOS name of a domain
使用 ActiveDs COM 库,它具有内置的名称翻译,可以工作并且不会做出任何假设(就像此处的其他答案一样)。
Use the ActiveDs COM library, it has built-in name translation that works and does not make any assumptions (like other answers here).
您可以在 user.DistinguishedName 属性中查找可能的域。
域 1 中的用户应包含字符串“DC=DOMAIN1”。它绝对不应该包含字符串“DC=DOMAIN2”。
You could look for the possible domains in the user.DistinguishedName property.
A user in Domain 1 should contain the string "DC=DOMAIN1". It definitely shouldn't contain the string "DC=DOMAIN2".
正如对该问题的评论之一中提到的,我认为这是最近的一个很好的答案:
As mentioned in one of the comments to the question I think this is a good answer for more recent times:
您是否尝试过将完全限定的域名传递给另一个应用程序?如果您执行
complete_qualified_domain\USER
,大多数 Windows API 都不会抱怨。Have you tried passing the fully qualified domain name to this other app? Most windows API's won't complain if you do
fully_qualified_domain\USER
.