Example #1
0
        public static async Task <string> Run(
            [HttpTrigger(
                 AuthorizationLevel.Function,
                 "get",
                 Route = null
                 )]
            HttpRequest req,
            [Token(
                 Identity = TokenIdentityMode.ClientCredentials,
                 IdentityProvider = "AAD",
                 Resource = "https://graph.microsoft.com"
                 )]
            string graphToken,
            ILogger log,
            ExecutionContext context)
        {
            var          azureFunctionsLogger = new AzureFunctionLogger(log);
            GraphService graphService         = new GraphService(graphToken, azureFunctionsLogger);

            var currUserItems = await graphService.GetUserFromSpUserListAsync(
                Configs.UserAdministrationGraphSiteId,
                Configs.UserAdministrationSharePointListId,
                true
                );

            var sendPasswordQueue   = new QueueService(Configs.QueueConnectionString, Configs.SendPasswordQueueName);
            var addUserToGroupQueue = new QueueService(Configs.QueueConnectionString, Configs.AddToGroupUsersQueueName);

            var exchangeOnlineService = new ExchangeOnlineService(
                addUserToGroupQueue
                );

            foreach (var currUserItem in currUserItems)
            {
                var user = GraphService.GetAdUserObjectFromUserListItem(currUserItem);

                await graphService.DeleteUserByPrincipalNameAsync(user.UserPrincipalName, false);

                var userId = await graphService.CreateUserAsync(user);

                var createdUser = await graphService.AssignE2LicenseToUserById(userId, Configs.DefaultO365UserLicense);

                await exchangeOnlineService.AddUserToGroupAsync(user.UserPrincipalName, Configs.DefaultExchangeGroupId);

                await sendPasswordQueue.CreateEncryptedMessageAsync($"{user.UserPrincipalName}|{user.PasswordProfile.Password}");
            }

            return("Email sent!");
        }
    static void Main(string[] args)
    {
        // var loggerEnvironment = "AzureFunctions";
        var     loggerEnvironment = "ConsoleApp";
        ILogger logger            = null;

        if (loggerEnvironment == "AzureFunctions")
        {
            Microsoft.Azure.WebJobs.Host.TraceWriter azureFunctionLogger = null;
            logger = new AzureFunctionLogger(azureFunctionLogger);
        }
        else if (loggerEnvironment == "ConsoleApp")
        {
            logger = new TraceLogger();
        }
        var doStuff = new DoStuff(logger);

        Console.ReadKey();
    }
Example #3
0
        public static void Run(
            [QueueTrigger(
                 "sendpasswordqueue-items",
                 Connection = "QueueConnectionString"
                 )]
            string myQueueItem,
            [Token(
                 Identity = TokenIdentityMode.ClientCredentials,
                 IdentityProvider = "AAD",
                 Resource = "https://graph.microsoft.com"
                 )]
            string graphToken,
            ILogger log,
            ExecutionContext context)
        {
            var azureFunctionsLogger = new AzureFunctionLogger(log);

            var decodedQueue      = Security.ToInsecureString(Security.DecryptString(myQueueItem)).Split('|');
            var userPrincipalName = decodedQueue[0];
            var userPassword      = decodedQueue[1];

            log.LogInformation($"Processing the password mail for user: {userPrincipalName}");

            var graphService = new GraphService(graphToken, azureFunctionsLogger);
            var emailService = new EmailService(graphService, Path.Combine(context.FunctionDirectory, "..", "Templates"));

            Task.Run(async() =>
            {
                var user         = await graphService.GetUserAsync(userPrincipalName);
                var admin        = await graphService.GetUserAsync(Configs.UserEmailSender);
                var userCopyMail = await graphService.GetUserAsync(Configs.UserEmailPasswordCopy);

                var isExchangeDeployed = await graphService.IsServicePlanFromUserActiveAndDeployed(userPrincipalName, Configs.DefaultExchangeOnlineLicense);

                if (!isExchangeDeployed)
                {
                    throw new Exception($"The Exchange is not yet assigned or deployed for user '{userPrincipalName}'");
                }

                await emailService.SendPasswordMailAsync(user, userCopyMail, admin, userPassword);
            }).GetAwaiter().GetResult();
        }