예제 #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!");
        }