我怎样才能在本地组的所有用户(具有良好的性能) [英] How can I get all users in a local group (with good performance)

查看:103
本文介绍了我怎样才能在本地组的所有用户(具有良好的性能)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找了好一段时间,但我发现所有的解决方案是非常慢的解决方案。
我想在本地Windows组的所有用户。这一组当然也包含AD组。所以结果应该包含本组本身成员的所有用户中那些包含在AD组的用户。
你知道这个具有良好性能的解决方案?

I looking for a solution for quite while but all solutions I found are very slow. I want to get all users in local windows group. This group can of course also contain AD groups. So the result should contain all users that are members of the group itself and the users of the AD groups that are contained. Do you know a solution for this with a good performance?

推荐答案

我做了一组类做到这一点而回,但基于域,而不是组得到了他们:)

Hey, i made a set of classes to do this a while back, but it got them based on Domain instead of group :)

下面是类。有一类的UserManager和User类

Here is the classes. There is a userManager class and a User class

public class UserManager
{
    private string _domainName;
    private Dictionary<string, User> _userLookup;
    private PrincipalContext domainContext;
    private DirectoryEntry LDAPdirectory;

    public UserManager(string domainName)
    {
        _domainName = domainName;
        _userLookup = new Dictionary<string, User>();
        domainContext = new PrincipalContext(ContextType.Domain, _domainName);
        //Make the LDAP directory look for all users within the domain. DC Com, Au for australia
        LDAPdirectory = new DirectoryEntry("LDAP://DC=" + _domainName.ToLower() + ",DC=com,DC=au");
        LDAPdirectory.AuthenticationType = AuthenticationTypes.Secure;
    }


    public IEnumerable<User> Users
    {
        get
        {
            return _userLookup.Values.ToArray<User>();
        }
        set
        {
            _userLookup.Clear();
            foreach (var user in value)
            {
                if (!_userLookup.ContainsKey(user.Login))
                    _userLookup.Add(user.Login, user);
            }
        }
    }

    /// <summary>
    /// Gets all the users from the AD domain and adds them to the Users property. Returns the list.
    /// </summary>
    /// <returns></returns>
    public IEnumerable<User> UpdateAllUsers()
    {

        DirectorySearcher searcher = new DirectorySearcher(LDAPdirectory);
        searcher.Filter = "(&(&(objectClass=user)(objectClass=person)(!objectClass=computer)(objectClass=organizationalPerson)(memberof=*)(telephonenumber=*)))";

        SearchResultCollection src = searcher.FindAll();
        _userLookup.Clear();

        foreach (SearchResult result in src)
        {
            User newUser = new User(domainContext, result.Properties["samaccountname"][0].ToString());

            if (newUser.IsInitialized)
            {
                _userLookup.Add(newUser.Login, newUser);
                yield return newUser;
            }
        }



    }
    public User GetUser(string userLogin)
    {
        return new User(domainContext, userLogin);
    }

    public bool HasUser(string login)
    {
        return _userLookup.ContainsKey(login);
    }
}

public class User
{
    public User()
    {
        IsInitialized = false;
    }
    /// <summary>
    /// Initializes a new user based on the AD info stored in the domain    
    /// </summary>
    /// <param name="domainContext">The domain to search for this user</param>
    /// <param name="userName">The user to look for</param>
    public User(PrincipalContext domainContext, string userName)
    {
        try
        {
            using (UserPrincipal thisUserPrincipal = UserPrincipal.FindByIdentity(domainContext, userName))
            {
                this.FirstName = thisUserPrincipal.GivenName;
                this.Surname = thisUserPrincipal.Surname;
                this.DisplayName = thisUserPrincipal.DisplayName;
                this.Email = thisUserPrincipal.EmailAddress;
                this.ContactNumber = thisUserPrincipal.VoiceTelephoneNumber;
                this.Login = thisUserPrincipal.SamAccountName;
                IsInitialized = true;
            }
        }
        catch (Exception)
        {
            IsInitialized = false;
            return;
        }
    }
    /// <summary>
    /// Gets a value determining if this user was properly initialized or if an exception was thrown during creation
    /// </summary>
    public bool IsInitialized { get; set; }
    public string FirstName { get; set; }
    public string Surname { get; set; }
    public string DisplayName { get; set; }
    public string Email { get; set; }
    public string Login { get; set; }
    public string ContactNumber { get; set; }
}

这篇关于我怎样才能在本地组的所有用户(具有良好的性能)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆