Example #1
0
        /// <summary>
        /// Ensures the service principal.
        /// </summary>
        /// <returns>PSADServicePrincipal.</returns>
        public PSADServicePrincipal EnsureServicePrincipal()
        {
            string applicationId = CurrentApplicationId.ToString();
            string appObjectId   = ActiveDirectoryClient.GetServicePrincipalsIdByAppId(CurrentApplicationId);
            PSADServicePrincipal servicePrincipal = ActiveDirectoryClient.GetServicePrincipalByObjectId(appObjectId);

            if (servicePrincipal == null)
            {
                VerboseLogger.Invoke(StorageSyncResources.CreateServicePrincipalMessage);
                // Create an application and get the applicationId
                var passwordCredential = new PSADPasswordCredential()
                {
                    StartDate = DateTime.Now,
                    EndDate   = DateTime.Now.AddYears(1),
                    KeyId     = Guid.NewGuid(),
                    Password  = SecureStringExtensions.ConvertToString(Guid.NewGuid().ToString().ConvertToSecureString())
                };

                var createParameters = new CreatePSServicePrincipalParameters
                {
                    ApplicationId       = CurrentApplicationId,
                    AccountEnabled      = bool.TrueString,
                    PasswordCredentials = new PSADPasswordCredential[]
                    {
                        passwordCredential
                    }
                };

                servicePrincipal = ActiveDirectoryClient.CreateServicePrincipal(createParameters);
            }

            return(servicePrincipal);
        }
        /// <summary>
        /// Ensures the service principal.
        /// </summary>
        /// <returns>PSADServicePrincipal.</returns>
        public PSADServicePrincipal EnsureServicePrincipal()
        {
            string applicationId = CurrentApplicationId.ToString();
            IEnumerable <PSADServicePrincipal> servicePrincipals = ActiveDirectoryClient.FilterServicePrincipals(new ODataQuery <ServicePrincipal>(s => s.AppId == applicationId));
            PSADServicePrincipal servicePrincipal = servicePrincipals.FirstOrDefault();

            if (servicePrincipal == null)
            {
                VerboseLogger.Invoke(StorageSyncResources.CreateServicePrincipalMessage);
                // Create an application and get the applicationId
                var passwordCredential = new PSADPasswordCredential()
                {
                    StartDate = DateTime.Now,
                    EndDate   = DateTime.Now.AddYears(1),
                    KeyId     = Guid.NewGuid(),
                    Password  = SecureStringExtensions.ConvertToString(Guid.NewGuid().ToString().ConvertToSecureString())
                };

                var createParameters = new CreatePSServicePrincipalParameters
                {
                    ApplicationId       = CurrentApplicationId,
                    AccountEnabled      = true,
                    PasswordCredentials = new PSADPasswordCredential[]
                    {
                        passwordCredential
                    }
                };

                servicePrincipal = ActiveDirectoryClient.CreateServicePrincipal(createParameters);
            }

            return(servicePrincipal);
        }
        /// <summary>
        /// Ensures the service principal.
        /// </summary>
        /// <returns>PSADServicePrincipal.</returns>
        public MicrosoftGraphServicePrincipal GetServicePrincipalOrNull()
        {
            string applicationId = CurrentApplicationId.ToString();
            // TODO: Remove this call once Az Powershell supports MSGraphClient in Test framework.
            MicrosoftGraphServicePrincipal servicePrincipal = this.StorageSyncResourceManager.GetServicePrincipalOrNull();

            if (servicePrincipal == null)
            {
                var oDataQuery = new ODataQuery <MicrosoftGraphServicePrincipal>(sp => sp.AppId == applicationId);
                servicePrincipal = MicrosoftGraphClient.FilterServicePrincipals(oDataQuery).FirstOrDefault();
            }
            return(servicePrincipal);
        }