仅获取属于特定组的用户

发布于 2024-12-08 07:44:00 字数 1173 浏览 0 评论 0原文

我正在尝试尽快了解 Active Directory,但到目前为止我还不太成功。我有这段代码,它返回其他人编写的 AD 中的所有用户,我应该更改它,以便它只返回指定组的用户。我尝试更多地了解 AD,希望能找到解决方案,但到目前为止还没有运气。那么也许有人可以帮助我?这是我的代码。

adSearch.Filter = "(&(objectClass=user))";
string groupName = System.Configuration.ConfigurationManager.AppSettings["ADGroupName"];
string domain = adSearch.SearchRoot.Properties["dc"].Value.ToString();
DomainLabel.Text = domain + " accounts:";
foreach (SearchResult sResultSet in adSearch.FindAll())
{
    if (!GetProperty(sResultSet, "givenName").Equals("") && !GetProperty(sResultSet, "sn").Equals(""))
    {
        string userAccountControl = GetProperty(sResultSet, "useraccountcontrol");
        bool x = userAccountControl.Equals("512") || userAccountControl.Equals("66048");
        if (x)
        {

           ListItem tempItem = new ListItem();
           unsortedList.Add(GetProperty(sResultSet, "givenName") + " " + GetProperty(sResultSet, "sn"));
           tempItem.Text = GetProperty(sResultSet, "givenName") + " " + GetProperty(sResultSet, "sn");
           tempItem.Value = GetProperty(sResultSet, "sAMAccountName");
           values.Add(tempItem);

        }
    }
}

I'm trying to understand Active Directory as quickly as I can, but so far I haven't been very successful. I have this code that returns all the users from AD which somebody else wrote and I'm supposed to alter it so that it only returns the users for a specified group. I've tried to learn a little bit more about AD in the hopes that the solution will present itself, but no luck so far. So maybe someone can help me out? Here's the code that I have.

adSearch.Filter = "(&(objectClass=user))";
string groupName = System.Configuration.ConfigurationManager.AppSettings["ADGroupName"];
string domain = adSearch.SearchRoot.Properties["dc"].Value.ToString();
DomainLabel.Text = domain + " accounts:";
foreach (SearchResult sResultSet in adSearch.FindAll())
{
    if (!GetProperty(sResultSet, "givenName").Equals("") && !GetProperty(sResultSet, "sn").Equals(""))
    {
        string userAccountControl = GetProperty(sResultSet, "useraccountcontrol");
        bool x = userAccountControl.Equals("512") || userAccountControl.Equals("66048");
        if (x)
        {

           ListItem tempItem = new ListItem();
           unsortedList.Add(GetProperty(sResultSet, "givenName") + " " + GetProperty(sResultSet, "sn"));
           tempItem.Text = GetProperty(sResultSet, "givenName") + " " + GetProperty(sResultSet, "sn");
           tempItem.Value = GetProperty(sResultSet, "sAMAccountName");
           values.Add(tempItem);

        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁对谁错谁最难过 2024-12-15 07:44:00

我已经这样做了,它检查用户名是否在安全组中。您可以获取 ActiveDirectory 中的用户,然后像这样检查它

public bool IsInSecurityGroup(string UserName)
    {
       bool _isInsecurityGroup;
                    string GroupName ="GroupName";
                    System.Security.Principal.WindowsIdentity MyIdentity = 
                    System.Security.Principal.WindowsIdentity.GetCurrent();
                    System.Security.Principal.WindowsPrincipal MyPrincipal = new 
                    System.Security.Principal.WindowsPrincipal(MyIdentity);

             return (MyPrincipal.IsInRole(GroupName)) ? true : false;


    }

对于检查多个用户,它应该适合您 从 SecurityGroup 获取用户
或此 GroupPrincipal.GetMembers 方法

I have done this in this way Where it checks the username if its in the SecurityGroup. You can Get the Users in the ActiveDirectory and then check it like this

public bool IsInSecurityGroup(string UserName)
    {
       bool _isInsecurityGroup;
                    string GroupName ="GroupName";
                    System.Security.Principal.WindowsIdentity MyIdentity = 
                    System.Security.Principal.WindowsIdentity.GetCurrent();
                    System.Security.Principal.WindowsPrincipal MyPrincipal = new 
                    System.Security.Principal.WindowsPrincipal(MyIdentity);

             return (MyPrincipal.IsInRole(GroupName)) ? true : false;


    }

For checking multiple users it should work for you Getting Users From SecurityGroup
or this GroupPrincipal.GetMembers Method

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文