public async Task CreateGalleryAppAsync(NewGalleryAppDetails newGalleryAppDetails)
        {
            // Step 1. Create the Gallery application
            var appServicePrincipal = await _galleryAppsRepository.CreateApplicationTemplate(newGalleryAppDetails.TemplateId, newGalleryAppDetails.DisplayName);

            // Step 2. Configure single sign-on
            Thread.Sleep(10000);
            string spoId = await ConfigureSingleSignOn(appServicePrincipal, newGalleryAppDetails);

            // Step 3. Configure claims mapping
            await ConfigureClaimsMapping(spoId, newGalleryAppDetails);

            // Step 4. Configure signing certificate
            await ConfigureSigningCertificate(spoId);
        }
        private async Task ConfigureClaimsMapping(string spoId, NewGalleryAppDetails newGalleryAppDetails)
        {
            // Read and assign the claims mapping policy definition
            string policyDefinition = System.IO.File.ReadAllText(newGalleryAppDetails.ClaimsMappingPolicyPath);

            var claimsMappingPolicy = new ClaimsMappingPolicy
            {
                Definition = new List <string>()
                {
                    policyDefinition
                },
                DisplayName = "automated-mappings-policy"
            };

            // Create and assign claims mapping policy
            await _galleryAppsRepository.ConfigureClaimsMappingPolicy(claimsMappingPolicy, spoId);
        }
        private async Task <string> ConfigureSingleSignOn(Beta.ApplicationServicePrincipal galleryApp, NewGalleryAppDetails newGalleryAppDetails)
        {
            // Create a service principal resource type with the desired configuration
            var servicePrincipal = new ServicePrincipal
            {
                PreferredSingleSignOnMode = newGalleryAppDetails.PreferredSsoMode,
                LoginUrl = newGalleryAppDetails.LoginUrl
            };

            // Create the webApplication resource type with the desired configuration. Be sure to replace the redirectUris
            var web = new WebApplication
            {
                RedirectUris = new string[] { newGalleryAppDetails.RedirectUri }
            };

            // Create an application resource type with the desired configuration. Be sure to replace the IdentifierUris
            var application = new Application
            {
                Web            = web,
                IdentifierUris = new string[] { newGalleryAppDetails.IdentifierUri }
            };

            string spoId  = galleryApp.ServicePrincipal.AdditionalData.First(x => x.Key == "objectId").Value.ToString();
            string appoId = galleryApp.Application.AdditionalData.First(x => x.Key == "objectId").Value.ToString();

            // Send servicePrincipal and Application to configure the applicationTemplate
            await _galleryAppsRepository.ConfigureApplicationTemplate(servicePrincipal, application, spoId, appoId);

            return(spoId);
        }