Пример #1
0
        /// <summary>
        /// Retrieve listing of all roles to which a specified user belongs.
        /// </summary>
        /// <param name="username"></param>
        /// <returns>String array of roles</returns>
        public override String[] GetRolesForUser(String username)
        {
            // If SQL Caching is enabled, try to pull a cached value.
            if (_EnableSqlCache)
            {
                String CachedValue;
                CachedValue = GetCacheItem('U', username);
                if (CachedValue != "*NotCached")
                {
                    return(CachedValue.Split(','));
                }
            }

            ArrayList results = new ArrayList();

            using (PrincipalContext context = new PrincipalContext(ContextType.Domain, null, _DomainDN))
            {
                try
                {
                    UserPrincipal p      = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, username);
                    var           groups = p.GetAuthorizationGroups();
                    foreach (GroupPrincipal group in groups)
                    {
                        if (!_GroupsToIgnore.Contains(group.SamAccountName))
                        {
                            if (_IsAdditiveGroupMode)
                            {
                                if (_GroupsToUse.Contains(group.SamAccountName))
                                {
                                    results.Add(group.SamAccountName);
                                }
                            }
                            else
                            {
                                results.Add(group.SamAccountName);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new ProviderException("Unable to query Active Directory.", ex);
                }
            }
            // If SQL Caching is enabled, send value to cache
            if (_EnableSqlCache)
            {
                SetCacheItem('U', username, ArrayListToCSString(results));
            }
            return(results.ToArray(typeof(String)) as String[]);
        }
Пример #2
0
        /// <summary>
        /// Retrieve listing of all users in a specified role.
        /// </summary>
        /// <param name="rolename">String array of users</param>
        /// <returns></returns>
        public override String[] GetUsersInRole(String rolename)
        {
            if (!RoleExists(rolename))
            {
                throw new ProviderException(String.Format("The role '{0}' was not found.", rolename));
            }
            // If SQL Caching is enabled, try to pull a cached value.
            if (_EnableSqlCache)
            {
                String CachedValue;
                CachedValue = GetCacheItem('R', rolename);
                if (CachedValue != "*NotCached")
                {
                    return(CachedValue.Split(','));
                }
            }
            ArrayList results = new ArrayList();

            using (PrincipalContext context = new PrincipalContext(ContextType.Domain, "UNFCSD", _DomainDN))
            {
                try
                {
                    GroupPrincipal p     = GroupPrincipal.FindByIdentity(context, IdentityType.SamAccountName, rolename);
                    var            users = p.GetMembers(true);
                    foreach (UserPrincipal user in users)
                    {
                        if (!_UsersToIgnore.Contains(user.SamAccountName))
                        {
                            results.Add(user.SamAccountName);
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new ProviderException("Unable to query Active Directory.", ex);
                }
            }
            // If SQL Caching is enabled, send value to cache
            if (_EnableSqlCache)
            {
                SetCacheItem('R', rolename, ArrayListToCSString(results));
            }
            return(results.ToArray(typeof(String)) as String[]);
        }
Пример #3
0
        /// <summary>
        /// Retrieve listing of all roles.
        /// </summary>
        /// <returns>String array of roles</returns>
        public override string[] GetAllRoles()
        {
            // If SQL Caching is enabled, try to pull a cached value.
            if (_EnableSqlCache)
            {
                String CachedValue;
                CachedValue = GetCacheItem('L', "AllRoles");
                if (CachedValue != "*NotCached")
                {
                    return(CachedValue.Split(','));
                }
            }
            ArrayList results = new ArrayList();

            String[] roles = ADSearch(_ActiveDirectoryConnectionString, "(&(objectCategory=group)(|(groupType=-2147483646)(groupType=-2147483644)(groupType=-2147483640)))", "samAccountName");
            foreach (String strRole in roles)
            {
                if (!_GroupsToIgnore.Contains(strRole))
                {
                    if (_IsAdditiveGroupMode)
                    {
                        if (_GroupsToUse.Contains(strRole))
                        {
                            results.Add(strRole);
                        }
                    }
                    else
                    {
                        results.Add(strRole);
                    }
                }
            }
            // If SQL Caching is enabled, send value to cache
            if (_EnableSqlCache)
            {
                SetCacheItem('L', "AllRoles", ArrayListToCSString(results));
            }
            return(results.ToArray(typeof(String)) as String[]);
        }