簡體   English   中英

獲取本地組,而不是域用戶的主要組

[英]get local groups and not the primary groups for a domain user

我有一個代碼來獲取用戶所屬的組。

try 
        {
            DirectoryEntry adRoot = new DirectoryEntry(string.Format("WinNT://{0}", Environment.UserDomainName));

            DirectoryEntry user = adRoot.Children.Find(completeUserName, "User");                
            object obGroups = user.Invoke("Groups");
            foreach (object ob in (IEnumerable)obGroups)
            {
                // Create object for each group.
                DirectoryEntry obGpEntry = new DirectoryEntry(ob);
                listOfMyWindowsGroups.Add(obGpEntry.Name);
            }
        return true;
        }
        catch (Exception ex)
        {
            new GUIUtility().LogMessageToFile("Error in getting User MachineGroups = " + ex);
            return false;
        }

當我必須找到本地用戶的組但上面的代碼工作正常

對於域用戶,它返回一個值“域用戶”,它有點奇怪,因為它是2個本地組的一部分。

請幫助解決這個謎團。 謝謝

研究

我做了一些發現並得到了我正在返回域用戶的主要組

稱為“域用戶”組

但我真正想要的是域用戶所屬的本地機器組......我無法得到那個......任何建議

另一個使用LDAP的代碼

        string domain = Environment.UserDomainName;
        DirectoryEntry DE = new DirectoryEntry("LDAP://" + domain, null, null, AuthenticationTypes.Secure);
        DirectorySearcher search = new DirectorySearcher();

        search.SearchRoot = DE;         
        search.Filter = "(SAMAccountName=" + completeUserName + ")";  //Searches active directory for the login name

        search.PropertiesToLoad.Add("displayName");  // Once found, get a list of Groups

        try
        {
            SearchResult result = search.FindOne(); // Grab the records and assign them to result
            if (result != null)
            {
                DirectoryEntry theUser = result.GetDirectoryEntry();
                theUser.RefreshCache(new string[] { "tokenGroups" });
                foreach (byte[] resultBytes in theUser.Properties["tokenGroups"])
                {
                    System.Security.Principal.SecurityIdentifier mySID = new System.Security.Principal.SecurityIdentifier(resultBytes, 0);

                    DirectorySearcher sidSearcher = new DirectorySearcher();

                    sidSearcher.SearchRoot = DE;
                    sidSearcher.Filter = "(objectSid=" + mySID.Value + ")";
                    sidSearcher.PropertiesToLoad.Add("distinguishedName");

                    SearchResult sidResult = sidSearcher.FindOne();

                    if (sidResult != null)
                    {
                        listOfMyWindowsGroups.Add((string)sidResult.Properties["distinguishedName"][0]);
                    }
                }
            }
            else
            {
                new GUIUtility().LogMessageToFile("no user found");

            }
            return true;
        }

        catch (Exception ex)
        {

            new GUIUtility().LogMessageToFile("Error obtaining group names: " + ex.Message + " Please contact your administrator."); // If an error occurs report it to the user.
            return false;
        }

這也有效,但我得到相同的結果“域用戶”。 請問some1告訴我如何獲得本地機器組... ????

如果您使用的是.NET 3.5,則可以使用System.DirectoryService.AccountManagement執行所有用戶和組管理。 特別是UserPrincipal.GetAuthorizationGroups正是您正在尋找的。 它為特定用戶檢索本地​​組和計算機組。 如果該組是本地組,則GroupPrincipal.Context.Name將顯示該組來自的計算機名稱。 如果該組是域組,則GroupPrincipal.Context.Domain將顯示該組所在的域名。

PrincipalContext context = new PrincipalContext(ContextType.Domain, "yourdomain.com");
UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, "youruser"); 

foreach (GroupPrincipal group in userPrincipal.GetAuthorizationGroups())
{
    Console.Out.WriteLine("{0}\\{1}", group.Context.Name, group.SamAccountName);
}

我會說問題是你的搜索是從域開始的。 您想要將搜索的位置更改為本地計算機。

像這樣的東西會這樣做;

DirectoryEntry AD = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");

暫無
暫無

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

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