确定的WindowsIdentity实例的嵌套组 [英] Determine nested groups of WindowsIdentity instance
问题描述
说我有的WindowsIdentity
的实例,并希望得到组织它的成员。我用下面的code,以获得清单:
Say i have an instance of WindowsIdentity
and want to get groups it's a member of. I use the following code to obtain the list:
WindowsIdentity identity = null;
// get identity here
identity.Groups.Translate(typeof(NTAccount)).Select(x => x.Value);
我得到这样的:
i get something like this:
"BUILTIN\\Administrators"
"BUILTIN\\Users"
"NT AUTHORITY\\INTERACTIVE"
"CONSOLE LOGON"
我有一个本地组(例如, MYSPECIALGROUP
),有 BUILTIN \\管理员
作为其成员。 MYSPECIALGROUP
未在样本中返回上面。我如何获得的所有的群体,包括嵌套的呢?
I have a local group (say, MYSPECIALGROUP
) that has BUILTIN\\Administrators
as its member. MYSPECIALGROUP
is not returned in the sample above. How do i get all groups including the nested ones?
推荐答案
<一个href="http://stackoverflow.com/questions/1974023/get-a-users-group-memberships-from-active-directory">Get从Active Directory 用户的组成员
由于这个问题的答案解释, System.DirectoryServices.AccountManagement
命名空间是你所需要的:
As the answer to that question explains, System.DirectoryServices.AccountManagement
namespace is what you need:
// get the user identity / roles
PrincipalContext pCtx = new PrincipalContext(ContextType.Domain,
Settings.Default.Domain, // domain
Settings.Default.DomainReadUser, // user to access AD with
Settings.Default.DomainReadPass); // password of that user
UserPrincipal user = UserPrincipal.FindByIdentity(pCtx,
User.Identity.Name.Split('\\').Last()); // Windows Auth current user
// this will have all of the security groups, even nested ones
IEnumerable<Principal> userRoles = user.GetAuthorizationGroups();
既然你似乎在做本地计算机的用户/组,并与您的WindowsIdentity变量,你想的前几行更改为:
Since you seem to be doing local machine users/groups, and with your WindowsIdentity variable, you would want to change the first few lines to:
PrincipalContext pCtx = new PrincipalContext(ContextType.Machine);
UserPrincipal user = UserPrincipal.FindByIdentity(pCtx,
identity.Name.Split('\\').Last());
另请参阅:管理目录安全主体在.NET Framework 3.5
这篇关于确定的WindowsIdentity实例的嵌套组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!