コード例 #1
0
        public override void SetTokenCacheForProfile(IAzureContextContainer profile)
        {
            base.SetTokenCacheForProfile(profile);
            var session = AzureSession.Instance;
            var cache   = new ProtectedFileTokenCache(Path.Combine(session.TokenCacheDirectory, session.TokenCacheFile), session.DataStore);

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

            profile.SetTokenCache(cache);
        }
コード例 #2
0
        void EnableAutosave(IAzureSession session, bool writeAutoSaveFile, out ContextAutosaveSettings result)
        {
            var    store       = session.DataStore;
            string contextPath = Path.Combine(session.ARMProfileDirectory, session.ARMProfileFile);
            string tokenPath   = Path.Combine(session.TokenCacheDirectory, session.TokenCacheFile);

            if (!IsValidPath(contextPath))
            {
                throw new PSInvalidOperationException(string.Format("'{0}' is not a valid path. You cannot enable context autosave without a valid context path", contextPath));
            }

            if (!IsValidPath(tokenPath))
            {
                throw new PSInvalidOperationException(string.Format("'{0}' is not a valid path. You cannot enable context autosave without a valid token cache path", tokenPath));
            }

            result = new ContextAutosaveSettings
            {
                CacheDirectory   = session.TokenCacheDirectory,
                CacheFile        = session.TokenCacheFile,
                ContextDirectory = session.ARMProfileDirectory,
                ContextFile      = session.ARMProfileFile,
                Mode             = ContextSaveMode.CurrentUser
            };

            FileUtilities.DataStore    = session.DataStore;
            session.ARMContextSaveMode = ContextSaveMode.CurrentUser;
            var diskCache = session.TokenCache as ProtectedFileTokenCache;

            try
            {
                if (diskCache == null)
                {
                    var memoryCache = session.TokenCache as AuthenticationStoreTokenCache;
                    try
                    {
                        FileUtilities.EnsureDirectoryExists(session.TokenCacheDirectory);

                        diskCache = new ProtectedFileTokenCache(tokenPath, store);
                        if (memoryCache != null && memoryCache.Count > 0)
                        {
                            diskCache.Deserialize(memoryCache.Serialize());
                        }

                        session.TokenCache = diskCache;
                    }
                    catch
                    {
                        // leave the token cache alone if there are file system errors
                    }
                }

                if (writeAutoSaveFile)
                {
                    try
                    {
                        FileUtilities.EnsureDirectoryExists(session.ProfileDirectory);
                        string autoSavePath = Path.Combine(session.ProfileDirectory, ContextAutosaveSettings.AutoSaveSettingsFile);
                        session.DataStore.WriteFile(autoSavePath, JsonConvert.SerializeObject(result));
                    }
                    catch
                    {
                        // do not fail for file system errors in writing the autosave setting
                    }
                }
            }
            catch
            {
                // do not throw if there are file system error
            }
        }