示例#1
0
        /// <summary>
        /// Convert a context from a PSObject
        /// </summary>
        /// <param name="other"></param>
        public PSAzureContext(PSObject other)
        {
            if (other == null || other.Properties == null)
            {
                throw new ArgumentNullException(nameof(other));
            }
            PSObject property;

            if (other.TryGetProperty(nameof(Account), out property))
            {
                Account = new PSAzureRmAccount(property);
            }
            if (other.TryGetProperty(nameof(Environment), out property))
            {
                Environment = new PSAzureEnvironment(property);
            }
            if (other.TryGetProperty(nameof(Subscription), out property))
            {
                Subscription = new PSAzureSubscription(property);
            }
            if (other.TryGetProperty(nameof(Tenant), out property))
            {
                Tenant = new PSAzureTenant(property);
            }
            if (other.TryGetProperty(nameof(TokenCache), out property))
            {
                AzureTokenCache cache = new AzureTokenCache();
                cache.Populate(property);
                TokenCache = new AuthenticationStoreTokenCache(cache);
            }

            VersionProfile = other.GetProperty <string>(nameof(VersionProfile));
            this.PopulateExtensions(other);
        }
        void DisableAutosave(IAzureSession session, bool writeAutoSaveFile, out ContextAutosaveSettings result)
        {
            var    store     = session.DataStore;
            string tokenPath = Path.Combine(session.TokenCacheDirectory, session.TokenCacheFile);

            result = new ContextAutosaveSettings
            {
                Mode = ContextSaveMode.Process
            };

            FileUtilities.DataStore    = session.DataStore;
            session.ARMContextSaveMode = ContextSaveMode.Process;
            var memoryCache = session.TokenCache as AuthenticationStoreTokenCache;

            if (memoryCache == null)
            {
                var diskCache = session.TokenCache as ProtectedFileTokenCache;
                memoryCache = new AuthenticationStoreTokenCache(new AzureTokenCache());
                if (diskCache != null && diskCache.Count > 0)
                {
                    memoryCache.Deserialize(diskCache.Serialize());
                }

                session.TokenCache = memoryCache;
            }

            if (writeAutoSaveFile)
            {
                FileUtilities.EnsureDirectoryExists(session.ProfileDirectory);
                string autoSavePath = Path.Combine(session.ProfileDirectory, ContextAutosaveSettings.AutoSaveSettingsFile);
                session.DataStore.WriteFile(autoSavePath, JsonConvert.SerializeObject(result));
            }
        }
        public void TokenCacheUsesValidData()
        {
            var store = new AzureTokenCache {
                CacheData = new byte[] { 2, 0, 0, 0, 0, 0, 0, 0 }
            };
            var cache = new AuthenticationStoreTokenCache(store);

            Assert.Equal(cache.CacheData, store.CacheData);
        }
        public void TokenCacheIgnoresInvalidData()
        {
            var store = new AzureTokenCache {
                CacheData = new byte[] { 3, 0, 0, 0, 0, 0, 0, 0 }
            };
            var cache = new AuthenticationStoreTokenCache(store);

            Assert.NotEqual(cache.CacheData, store.CacheData);
        }
        public override void SetTokenCacheForProfile(IAzureContextContainer profile)
        {
            base.SetTokenCacheForProfile(profile);
            var cache = new AuthenticationStoreTokenCache(TokenCache.DefaultShared);

            if (profile.HasTokenCache())
            {
                cache.Deserialize(profile.GetTokenCache().CacheData);
            }

            AzureSession.Instance.TokenCache = cache;
            profile.SetTokenCache(cache);
        }
        private void DisableAutosave(IAzureSession session)
        {
            session.ARMContextSaveMode = ContextSaveMode.Process;
            var memoryCache = session.TokenCache as AuthenticationStoreTokenCache;

            if (memoryCache == null)
            {
                var diskCache = session.TokenCache as ProtectedFileTokenCache;
                memoryCache = new AuthenticationStoreTokenCache(new AzureTokenCache());
                if (diskCache != null && diskCache.Count > 0)
                {
                    memoryCache.Deserialize(diskCache.Serialize());
                }

                session.TokenCache = memoryCache;
            }
        }