如何从GroupPrincipal获得域名? [英] How to get Domain from GroupPrincipal?
问题描述
我需要列出所有用户按以下格式特定的本地组:域\用户名。我可以提取集合GroupPrincipal对象为组,但我不知道如何获得用户需要的格式。 GroupPrincipal没有属性域。
I need to list all users from the specific local group in the following format: "Domain\UserName". I can extract collection of GroupPrincipal objects for the group, but I don't know how to get users in required format. GroupPrincipal doesn't have property Domain.
下面code输出用户无需域名(如用户名)。
The following code outputs users without domain (e.g. "UserName").
using (var context = new PrincipalContext(ContextType.Machine, null))
{
using (var group = GroupPrincipal.FindByIdentity(context, IdentityType.SamAccountName, @"My Local Group"))
{
if (group != null)
{
foreach (var p in group.GetMembers(false))
{
Console.WriteLine(p.SamAccountName);
}
}
}
}
是否有可能从主体对象获取域名NetBIOS名称?如果是这样,怎么弄呢?
Is it possible to get domain netbios name from the principal object? And if so, how to get it?
推荐答案
您可以从委托人的背景下域的详细信息。例如:
You can get the domain details from the principal's Context. e.g.:
foreach (var p in group.GetMembers(false))
{
Console.Write(p.SamAccountName);
if (p.ContextType == ContextType.Domain)
{
Console.Write(" ({0})", p.Context.Name);
}
Console.WriteLine();
}
如果你只是想输出帐户名从一台机器上的域的域\用户的格式,你可以翻译校长的SecurityIdentifier到NTACCOUNT。例如:
If you just want to output account names in the "domain\user" format from a machine on the domain, you can translate the principal's SecurityIdentifier to an NTAccount. e.g.:
foreach (var p in group.GetMembers(false))
{
Console.WriteLine(p.Sid.Translate(typeof(NTAccount)).ToString());
}
这篇关于如何从GroupPrincipal获得域名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!