Exemplo n.º 1
0
        static ContextAutosaveSettings InitializeSessionSettings(IDataStore store, string cacheDirectory, string profileDirectory, string settingsFile, bool migrated = false)
        {
            var result = new ContextAutosaveSettings
            {
                CacheDirectory   = cacheDirectory,
                ContextDirectory = profileDirectory,
                Mode             = ContextSaveMode.Process,
                CacheFile        = "msal.cache",
                ContextFile      = "AzureRmContext.json"
            };

            var settingsPath = Path.Combine(profileDirectory, settingsFile);

            try
            {
                if (store.FileExists(settingsPath))
                {
                    var settingsText = store.ReadFileAsText(settingsPath);
                    ContextAutosaveSettings settings = JsonConvert.DeserializeObject <ContextAutosaveSettings>(settingsText);
                    result.CacheDirectory   = migrated ? cacheDirectory : settings.CacheDirectory == null ? cacheDirectory : string.Equals(settings.CacheDirectory, profileDirectory) ? cacheDirectory : settings.CacheDirectory;
                    result.CacheFile        = settings.CacheFile == null ? result.CacheFile : string.Equals(settings.CacheFile, "TokenCache.dat") ? result.CacheFile : settings.CacheFile;
                    result.ContextDirectory = migrated ? profileDirectory : settings.ContextDirectory ?? result.ContextDirectory;
                    result.Mode             = settings.Mode;
                    result.ContextFile      = settings.ContextFile ?? result.ContextFile;
                    if (migrated)
                    {
                        string autoSavePath = Path.Combine(profileDirectory, settingsFile);
                        store.WriteFile(autoSavePath, JsonConvert.SerializeObject(result));
                    }
                }
                else
                {
                    string directoryPath = Path.GetDirectoryName(profileDirectory);
                    if (!store.DirectoryExists(directoryPath))
                    {
                        store.CreateDirectory(directoryPath);
                    }
                    string autoSavePath = Path.Combine(profileDirectory, settingsFile);
                    result.Mode = ContextSaveMode.CurrentUser;
                    store.WriteFile(autoSavePath, JsonConvert.SerializeObject(result));
                }
            }
            catch (Exception ex)
            {
                // ignore exceptions in reading settings from disk
                result.Mode = ContextSaveMode.Process;
                TracingAdapter.Information("[AzureSessionInitializer]: Cannot read settings from disk. Falling back to 'Process' mode.");
                TracingAdapter.Information($"[AzureSessionInitializer]: Message: {ex.Message}; Stacktrace: {ex.StackTrace}");
            }

            return(result);
        }
Exemplo n.º 2
0
        static ContextAutosaveSettings InitializeSessionSettings(IDataStore store, string profileDirectory, string settingsFile, bool migrated = false)
        {
            var result = new ContextAutosaveSettings
            {
                CacheDirectory   = profileDirectory,
                ContextDirectory = profileDirectory,
                Mode             = ContextSaveMode.Process,
                CacheFile        = "TokenCache.dat",
                ContextFile      = "AzureRmContext.json"
            };

            var settingsPath = Path.Combine(profileDirectory, settingsFile);

            try
            {
                if (store.FileExists(settingsPath))
                {
                    var settingsText = store.ReadFileAsText(settingsPath);
                    ContextAutosaveSettings settings = JsonConvert.DeserializeObject <ContextAutosaveSettings>(settingsText);
                    result.CacheDirectory   = migrated ? profileDirectory : settings.CacheDirectory ?? result.CacheDirectory;
                    result.CacheFile        = settings.CacheFile ?? result.CacheFile;
                    result.ContextDirectory = migrated ? profileDirectory : settings.ContextDirectory ?? result.ContextDirectory;
                    result.Mode             = settings.Mode;
                    result.ContextFile      = settings.ContextFile ?? result.ContextFile;
                    if (migrated)
                    {
                        string autoSavePath = Path.Combine(profileDirectory, settingsFile);
                        store.WriteFile(autoSavePath, JsonConvert.SerializeObject(result));
                    }
                }
                else
                {
                    string directoryPath = Path.GetDirectoryName(profileDirectory);
                    if (!store.DirectoryExists(directoryPath))
                    {
                        store.CreateDirectory(directoryPath);
                    }
                    string autoSavePath = Path.Combine(profileDirectory, settingsFile);
                    result.Mode = ContextSaveMode.CurrentUser;
                    store.WriteFile(autoSavePath, JsonConvert.SerializeObject(result));
                }
            }
            catch
            {
                // ignore exceptions in reading settings from disk
                result.Mode = ContextSaveMode.Process;
            }

            return(result);
        }