Пример #1
0
        public static ISet <string> GetRoles(this WindowsIdentity windowsIdentity, WindowsRolesOptions options = null)
        {
            if (windowsIdentity == null)
            {
                throw new ArgumentNullException(nameof(windowsIdentity));
            }

            return(windowsIdentity.Groups.AsRoles(options));
        }
        public static ISet <string> AsRoles(this IdentityReferenceCollection identityReferences, WindowsRolesOptions options = null)
        {
            if (identityReferences == null)
            {
                throw new ArgumentNullException(nameof(identityReferences));
            }

            options ??= new WindowsRolesOptions();

            // ReSharper disable AssignNullToNotNullAttribute
            var securityIdentifiers = identityReferences.Cast <SecurityIdentifier>();

            // ReSharper restore AssignNullToNotNullAttribute

            if (!options.BuiltInRolesEnabled)
            {
                securityIdentifiers = securityIdentifiers.Where(securityIdentifier => securityIdentifier.AccountDomainSid != null);
            }

            identityReferences = new IdentityReferenceCollection();

            foreach (var securityIdentifier in securityIdentifiers)
            {
                identityReferences.Add(securityIdentifier);
            }

            // ReSharper disable AssignNullToNotNullAttribute
            var roles = identityReferences.Translate(typeof(NTAccount)).Select(ntAccount => ntAccount.Value);

            // ReSharper restore AssignNullToNotNullAttribute

            if (!options.MachineRolesEnabled)
            {
                roles = roles.Where(role => !role.StartsWith($"{Environment.MachineName}\\", StringComparison.OrdinalIgnoreCase));
            }

            return(new SortedSet <string>(roles, StringComparer.OrdinalIgnoreCase));
        }