sexta-feira, 24 de julho de 2015

ActiveDirectoryGroup C#

using System;
using System.DirectoryServices;
using System.Linq;

namespace Lapuinka.Security.ActiveDirectory
{
    public class ActiveDirectoryGroup : ActiveDirectoryObject
    {
        private readonly DirectoryEntry _entry;
        private ActiveDirectoryUser[] _members;

        public string Name
        {
            get { return (string) _entry.Properties["name"].Value; }
        }

        public ActiveDirectoryUser[] Members
        {
            get
            {
                if (_members == null)
                {
                    try
                    {
                        if (_entry.Properties["member"].Value is object[])
                        {
                            _members = ((object[])_entry.Properties["member"].Value)
                                .Cast<string>()
                                .Select(g => new ActiveDirectoryUser(Create(g)))
                                .ToArray();
                        }
                        else if (_entry.Properties["member"].Value is string)
                        {
                            _members = new[]
                                          {
                                              new ActiveDirectoryUser(
                                                  Create((string)_entry.Properties["member"].Value))
                                          };
                        }
                    }
                    catch (Exception)
                    {
                        return new ActiveDirectoryUser[0];
                    }
                   
                }

                return _members ?? new ActiveDirectoryUser[0];
            }
        }

        public ActiveDirectoryGroup(DirectoryEntry entry)
        {
            _entry = entry;
        }
    }
}


Postar um comentário