Exemple #1
0
        /// <summary>
        /// Gets a list of the roles that a specified user is in for the configured applicationName.
        /// </summary>
        /// <param name="username">The user to return a list of roles for.</param>
        /// <returns>A string array containing the names of all the roles that the specified user is in for the configured applicationName.</returns>
        public override string[] GetRolesForUser(string username)
        {
            // Takes as input a user name and returns the role names that the specified user is associated with, from the data source. Only the roles for the configured ApplicationName are retrieved.
            // If no roles exist for the specified user for the configured ApplicationName, you should return a string array with no elements.
            // You should throw an ArgumentException if the specified user name is an empty string. You should throw an ArgumentNullException if the specified user name is null (Nothing in Visual Basic).

            if (!string.IsNullOrWhiteSpace(username))
            {
                // The list of roles that the user belongs to.
                List <string> roles = new List <string>();

                // Check whether the user's GUID is found.
                // If there is an error accessing the user's information all role processing will be skipped.
                Guid userGuid = ad.GetGUIDBySAMAccountName(username);
                if (userGuid != Guid.Empty)
                {
                    User          user     = new User(ad, userGuid);
                    List <string> groupDns = user.Groups;

                    // Get the SAMAccountName of each group the user is a member of and add it as a role.
                    foreach (string groupDn in groupDns)
                    {
                        // Check that the group's entry was found by its distinguished name.
                        // If there is an error accessing the group's information, the group will not be added to the list of roles.
                        System.DirectoryServices.Protocols.SearchResultEntry groupEntry = ad.GetEntryByDistinguishedName(groupDn);
                        if (groupEntry != null)
                        {
                            Group group = new Group(ad, groupEntry);
                            roles.Add(group.SAMAccountName);
                        }
                    }
                }

                // Return the list of roles to which the user belongs.
                return(roles.ToArray());
            }
            else
            {
                if (username != null)
                {
                    throw new ArgumentException("username can not be empty.", nameof(username));
                }
                else
                {
                    throw new ArgumentNullException(nameof(username));
                }
            }
        }
        public override object Transform(System.DirectoryServices.Protocols.SearchResultEntry entry)
        {
            var transformed = _selectProjection.Projection.DynamicInvoke(base.Transform(entry));

            return(transformed);
        }