Esempio n. 1
0
        private static List <UserPrincipalExtension> GetEnabledDomainUsers(string domainName)
        {
            _logger.Information($"Run user scanning in domain: '{domainName}'.");

            var myDomainUsers = new List <UserPrincipalExtension>();

            using (var ctx = new PrincipalContext(ContextType.Domain, domainName))
            {
                var userPrinciple = new UserPrincipalExtension(ctx);
                using var search = new PrincipalSearcher(userPrinciple);

                // Filter only active users
                userPrinciple.Enabled = true;
                search.QueryFilter    = userPrinciple;

                foreach (var domainUser in search.FindAll())
                {
                    if (domainUser.DisplayName != null)
                    {
                        myDomainUsers.Add((UserPrincipalExtension)domainUser);
                    }
                }
            }

            _logger.Information($"User scanning finished. Total enabled users found: '{myDomainUsers.Count}'.");

            return(myDomainUsers);
        }
Esempio n. 2
0
        private static async Task SendBrthAsync(SendGridMessage mail, UserPrincipalExtension user)
        {
            // Creating client and sending messages.
            var client = new SendGridClient(_config.GetSection("SendGrid:ApiKey").Value);
            var result = await client.SendEmailAsync(mail);

            // Logging results
            if (result.StatusCode != HttpStatusCode.Accepted)
            {
                _logger.Error($"Unable send email to: '{user.EmailAddress}'. Status code '{result.StatusCode}'.");
            }
            else if (result.StatusCode == HttpStatusCode.Accepted)
            {
                _logger.Information($"Mail has been sent to: '{user.EmailAddress}'.");
            }
        }