void CopyProfile(AzureRmProfile source, IProfileOperations target)
        {
            if (source == null || target == null)
            {
                return;
            }

            foreach (var environment in source.Environments)
            {
                IAzureEnvironment merged;
                target.TrySetEnvironment(environment, out merged);
            }

            foreach (var context in source.Contexts)
            {
                target.TrySetContext(context.Key, context.Value);
            }

            if (!string.IsNullOrWhiteSpace(source.DefaultContextKey))
            {
                target.TrySetDefaultContext(source.DefaultContextKey);
            }

            AzureRmProfileProvider.Instance.SetTokenCacheForProfile(target.ToProfile());
            EnsureProtectedCache(target, source.DefaultContext?.TokenCache?.CacheData);
        }
        void CopyProfile(AzureRmProfile source, IProfileOperations target)
        {
            if (source == null || target == null)
            {
                return;
            }

            foreach (var environment in source.Environments)
            {
                IAzureEnvironment merged;
                target.TrySetEnvironment(environment, out merged);
            }

            foreach (var context in source.Contexts)
            {
                target.TrySetContext(context.Key, context.Value);
            }

            if (!string.IsNullOrWhiteSpace(source.DefaultContextKey))
            {
                target.TrySetDefaultContext(source.DefaultContextKey);
            }

            EnsureProtectedMsalCache();
        }
        void CopyProfile(AzureRmProfile source, IProfileOperations target)
        {
            if (source == null || target == null)
            {
                return;
            }

            foreach (var environment in source.Environments)
            {
                IAzureEnvironment merged;
                target.TrySetEnvironment(environment, out merged);
            }
            if (!AzureSession.Instance.TryGetComponent(AzKeyStore.Name, out AzKeyStore keyStore))
            {
                keyStore = null;
            }
            foreach (var context in source.Contexts)
            {
                target.TrySetContext(context.Key, context.Value);
                if (keyStore != null)
                {
                    var account = context.Value.Account;
                    if (account != null)
                    {
                        var secret = account.GetProperty(AzureAccount.Property.ServicePrincipalSecret);
                        if (!string.IsNullOrEmpty(secret))
                        {
                            keyStore.SaveKey(new ServicePrincipalKey(AzureAccount.Property.ServicePrincipalSecret, account.Id, context.Value.Tenant?.Id)
                                             , secret.ConvertToSecureString());
                        }
                        var password = account.GetProperty(AzureAccount.Property.CertificatePassword);
                        if (!string.IsNullOrEmpty(password))
                        {
                            keyStore.SaveKey(new ServicePrincipalKey(AzureAccount.Property.CertificatePassword, account.Id, context.Value.Tenant?.Id)
                                             , password.ConvertToSecureString());
                        }
                    }
                }
            }

            if (!string.IsNullOrWhiteSpace(source.DefaultContextKey))
            {
                target.TrySetDefaultContext(source.DefaultContextKey);
            }

            EnsureProtectedMsalCache();
        }
Exemplo n.º 4
0
        public IAzureEnvironment AddOrSetEnvironment(IAzureEnvironment environment)
        {
            if (environment == null)
            {
                throw new ArgumentNullException("environment", AuthenticationMessages.EnvironmentNeedsToBeSpecified);
            }

            if (AzureEnvironment.PublicEnvironments.ContainsKey(environment.Name))
            {
                throw new InvalidOperationException(
                          string.Format(AuthenticationMessages.ChangingDefaultEnvironmentNotSupported, "environment"));
            }

            IAzureEnvironment result = null;

            _profile.TrySetEnvironment(environment, out result);
            return(result);
        }