Configuration for generating an Azure Profile using certificate or AAD credentials
        private void InitializeAzureProfile(AzureProfile profile, string parameterSet, AzureProfileSettings settings)
        {
            var savedCache = AzureSession.TokenCache;
            AzureSession.TokenCache = DefaultMemoryTokenCache;
            try
            {

                var profileClient = new ProfileClient(profile);
                if (settings.Environment == null)
                {
                    settings.Environment = AzureEnvironment.PublicEnvironments["AzureCloud"];
                }
                switch (parameterSet)
                {
                    case CertificateParameterSet:
                        profileClient.InitializeProfile(settings.Environment, new Guid(settings.SubscriptionId),
                            settings.Certificate,
                            settings.StorageAccount);
                        break;
                    case CredentialsParameterSet:
                        var userAccount = new AzureAccount
                        {
                            Id = settings.Credential.UserName,
                            Type = AzureAccount.AccountType.User
                        };
                        profileClient.InitializeProfile(settings.Environment, new Guid(settings.SubscriptionId),
                            userAccount,
                            settings.Credential.Password, settings.StorageAccount);
                        break;
                    case AccessTokenParameterSet:
                        profileClient.InitializeProfile(settings.Environment, new Guid(settings.SubscriptionId),
                            settings.AccessToken,
                            settings.AccountId, settings.StorageAccount);
                        break;
                    case ServicePrincipalParameterSet:
                        var servicePrincipalAccount = new AzureAccount
                        {
                            Id = settings.Credential.UserName,
                            Type = AzureAccount.AccountType.ServicePrincipal,
                        };
                        servicePrincipalAccount.SetOrAppendProperty(AzureAccount.Property.Tenants, settings.Tenant);
                        profileClient.InitializeProfile(settings.Environment, new Guid(settings.SubscriptionId),
                            servicePrincipalAccount,
                            settings.Credential.Password, settings.StorageAccount);
                        break;
                    case EmptyParameterSet:
                        if (!profile.Environments.ContainsKey(settings.Environment.Name))
                        {
                            profile.Environments.Add(settings.Environment.Name, settings.Environment);
                        }
                        break;
                }
            }
            finally
            {
                AzureSession.TokenCache = savedCache;
            }
        }
        private string ParseHashTableParameters(Hashtable propertyBag, out AzureProfileSettings settings)
        {
            settings = new AzureProfileSettings();
            string parametSetName = null;
            if (!propertyBag.ContainsKey(SubscriptionIdKey))
            {
                throw new ArgumentException(Resources.MissingSubscriptionInProfileProperties);
            }

            settings.SubscriptionId = (string)propertyBag[SubscriptionIdKey];
            if (propertyBag.ContainsKey(StorageAccountKey))
            {
                settings.StorageAccount = (string)propertyBag[StorageAccountKey];
            }

            if (propertyBag.ContainsKey(EnvironmentKey))
            {
                var environmentValue = (string)propertyBag[EnvironmentKey];
                if (AzureEnvironment.PublicEnvironments.ContainsKey(environmentValue))
                {
                    settings.Environment = AzureEnvironment.PublicEnvironments[environmentValue];
                }
            }

            if (propertyBag.ContainsKey(CertificateKey))
            {
                if (!propertyBag[CertificateKey].GetType().IsAssignableFrom(typeof(X509Certificate2)))
                {
                    throw new ArgumentException(Resources.MissingCertificateInProfileProperties);
                }

                settings.Certificate = (X509Certificate2)propertyBag[CertificateKey];
                parametSetName = CertificateParameterSet;
            }
            else if (propertyBag.ContainsKey(UsernameKey))
            {
                settings.Credential = CreatePsCredential((string)propertyBag[UsernameKey], propertyBag);
                if (propertyBag.ContainsKey(TenantKey))
                {
                    settings.Tenant = (string)propertyBag[TenantKey];
                }
                parametSetName = CredentialsParameterSet;
            }
            else if (propertyBag.ContainsKey(SPNKey) && propertyBag.ContainsKey(TenantKey))
            {
                settings.Credential = CreatePsCredential((string)propertyBag[SPNKey], propertyBag);
                if (propertyBag.ContainsKey(TenantKey))
                {
                    settings.Tenant = (string)propertyBag[TenantKey];
                }
                parametSetName = ServicePrincipalParameterSet;
            }
            else if (propertyBag.ContainsKey(AccountIdKey) && propertyBag.ContainsKey(TokenKey))
            {
                settings.AccountId = (string)propertyBag[AccountIdKey];
                settings.AccessToken = (string)propertyBag[TokenKey];
                parametSetName = AccessTokenParameterSet;
            }
            else
            {
                throw new ArgumentException(Resources.InvalidProfileProperties);
            }

            return parametSetName;
        }
 private void InitializeAzureProfile(AzureProfile profile, string parameterSet, AzureProfileSettings settings)
 {
     var profileClient = new ProfileClient(profile);
     if (settings.Environment == null)
     {
         settings.Environment = AzureEnvironment.PublicEnvironments["AzureCloud"];
     }
     switch (parameterSet)
     {
         case CertificateParameterSet:
             profileClient.InitializeProfile(settings.Environment, new Guid(settings.SubscriptionId), settings.Certificate,
                 settings.StorageAccount);
             break;
         case CredentialsParameterSet:
             var userAccount = new AzureAccount
             {
                 Id = settings.Credential.UserName,
                 Type = AzureAccount.AccountType.User
             };
             profileClient.InitializeProfile(settings.Environment, new Guid(settings.SubscriptionId), userAccount,
                 settings.Credential.Password, settings.StorageAccount);
             break;
         case AccessTokenParameterSet:
             profileClient.InitializeProfile(settings.Environment, new Guid(settings.SubscriptionId), settings.AccessToken,
                 settings.AccountId, settings.StorageAccount);
             break;
         case ServicePrincipalParameterSet:
             var servicePrincipalAccount = new AzureAccount
             {
                 Id = settings.Credential.UserName,
                 Type = AzureAccount.AccountType.ServicePrincipal
             };
             profileClient.InitializeProfile(settings.Environment, new Guid(settings.SubscriptionId), servicePrincipalAccount,
                 settings.Credential.Password, settings.StorageAccount);
             break;
     }
 }