コード例 #1
0
        public LicenseGroup GetGroup(Guid groupId)
        {
            using (var principalContext = new PrincipalContext(ContextType.Domain))
            {
                GroupPrincipal group = GetGroupPrincipal(principalContext, groupId);

                if (group.IsSecurityGroup == null)
                {
                    Logger.Warn($"Cannot tell if {group.GetDisplayText()} is a security group or not.");
                    return(null);
                }

                if (!bool.TryParse(group.IsSecurityGroup.ToString(), out bool _))
                {
                    Logger.Warn($"Cannot process {group.GetDisplayText()} because the IsSecurityGroup value is not valid");
                    return(null);
                }

                DateTimeOffset whenCreated;
                try
                {
                    string getWhenCreated = group.GetProperty("whenCreated");
                    if (getWhenCreated.IsNullOrEmpty())
                    {
                        throw new Exception($"WhenCreated property for {group.GetDisplayText()} is null or empty. Please make sure the service is running with correct permissions to access Active Directory.");
                    }

                    whenCreated = DateTimeOffset.Parse(getWhenCreated);
                }
                catch (Exception ex)
                {
                    Logger.Error($"Failed to determine the when created date for {group.GetDisplayText()}.");
                    Logger.Debug("Exception getting WhenCreated GroupPrincipal property.", ex);
                    throw new UserFriendlyException(ex.Message);
                }

                return(new LicenseGroup
                {
                    Id = groupId,
                    Name = group.Name,
                    WhenCreated = whenCreated
                });
            }
        }