簡體   English   中英

在沒有外部安全策略的本地組中搜索本地用戶

[英]Search for a Local user in a local group that does not have Foreign Security policy

基本上,我發現了一個帖子,該帖子為我們在應用程序中遇到的問題提供了解決方案,而解決方案是:

    private static void listGroupMembers(string groupDistinguishedName, PrincipalContext ctx, List<UserPrincipal> users)
{
    DirectoryEntry group = new DirectoryEntry("LDAP://" + groupDistinguishedName);
    foreach (string dn in group.Properties["member"])
    {

        DirectoryEntry gpMemberEntry = new DirectoryEntry("LDAP://" + dn);
        System.DirectoryServices.PropertyCollection userProps = gpMemberEntry.Properties;

        object[] objCls = (userProps["objectClass"].Value) as object[];

        if (objCls.Contains("group"))
            listGroupMembers(userProps["distinguishedName"].Value as string, ctx, users);

        if (!objCls.Contains("foreignSecurityPrincipal"))
        {                    
            UserPrincipal u = UserPrincipal.FindByIdentity(ctx, IdentityType.DistinguishedName, dn);
            if(u!=null)  // u==null for any other types except users
                users.Add(u);
        }
    }                 
}

但是我試圖搜索本地組,所以如果我更改目錄條目說:

DirectoryEntry groupEntry =
            new DirectoryEntry(string.Format("WinNT://{0}/{1},group", Environment.MachineName, groupName));

然后它不起作用,並說該屬性不存在。 除了本地組和用戶外,我該如何做?

為了解決這個問題,我最終做了:

protected bool IsUserInLocalGroup(string userName, string group)
    {
        using (DirectoryEntry computerEntry = new DirectoryEntry("WinNT://{0},computer".FormatWith(Environment.MachineName)))
        using(DirectoryEntry groupEntry = computerEntry.Children.Find(group, "Group"))
        {
            foreach (object o in (IEnumerable)groupEntry.Invoke("Members"))
            {
                using (DirectoryEntry entry = new DirectoryEntry(o))
                {
                    if (entry.SchemaClassName.Equals("User", StringComparison.OrdinalIgnoreCase) && entry.Name.Equals(userName, StringComparison.OrdinalIgnoreCase))
                    {
                        return true;
                    }
                }
            }
            return false;
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM