コード例 #1
0
        public bool IsCacheValid()
        {
            var file = ProtectedFile.GetCacheFile(_fileName);

            if (!File.Exists(file))
            {
                return(false);
            }

            try
            {
                var json = JObject.Parse(File.ReadAllText(file));
                if (Constants.FileVersion.Value != json.Value <string>("ver"))
                {
                    ClearAll();
                    return(false);
                }
                return(true);
            }
            catch (Exception)
            {
                ClearAll();
                return(false);
            }
        }
コード例 #2
0
        public void SaveRecentToken(TokenCacheInfo cacheInfo, string resource)
        {
            var file = ProtectedFile.GetCacheFile(GetRecentTokenFileName(resource));
            var json = JObject.FromObject(cacheInfo);

            ProtectedFile.WriteAllText(ProtectedFile.GetCacheFile(file), json.ToString());
        }
コード例 #3
0
        public static bool CommitDefaultSettings(this ApplicationOptions settings, bool waitWindow = true)
        {
            try
            {
                if (waitWindow)
                {
                    return((bool)WaitWindow.WaitWindow.Show(CommitDefaultSettings, @"Saving settings", settings));
                }
                else
                {
                    //write all new settings
                    var protectedFile = new ProtectedFile(SettingsFile);
                    protectedFile.WriteAllText(settings.ProfileToXml(), ProtectedSettings);

                    return(true);
                }
            }
            catch (Exception ex)
            {
                LoggingHelpers.RecordException(ex.Message, @"CommitToDefaultError");
            }

            //default
            return(false);
        }
コード例 #4
0
 public void ClearCache()
 {
     foreach (var filePath in Directory.GetFiles(ProtectedFile.GetCachePath(), "*token*", SearchOption.TopDirectoryOnly))
     {
         File.Delete(filePath);
     }
 }
コード例 #5
0
        public bool IsCacheValid()
        {
            var file = ProtectedFile.GetCacheFile(_fileName);

            if (!File.Exists(file))
            {
                return(false);
            }

            try
            {
                var json = JObject.Parse(File.ReadAllText(file));
                if (Constants.FileVersion.Value != json.Value <string>("ver"))
                {
                    ClearAll();
                    return(false);
                }

                AzureEnvironments unused;
                return(Enum.TryParse <AzureEnvironments>(json.Value <string>("env"), out unused));
            }
            catch (Exception)
            {
                ClearAll();
                return(false);
            }
        }
コード例 #6
0
        public void SaveRecentToken(TokenCacheInfo cacheInfo, string resource)
        {
            var file = ProtectedFile.GetCacheFile(resource == Constants.CSMResource ? _recentARMFileName : _recentAADFileName);
            var json = JObject.FromObject(cacheInfo);

            ProtectedFile.WriteAllText(ProtectedFile.GetCacheFile(file), json.ToString());
        }
コード例 #7
0
 public void ClearCache()
 {
     foreach (var filePath in Directory.GetFiles(Path.GetDirectoryName(ProtectedFile.GetCacheFile(_fileName)), "cache_tenants*", SearchOption.TopDirectoryOnly))
     {
         File.Delete(filePath);
     }
 }
コード例 #8
0
ファイル: GuidHandler.cs プロジェクト: mitchscobell/PlexDL
        /// <summary>
        /// Generates a new random global GUID and if storeNew is true, it replaces the existing GUID in persistent storage.
        /// </summary>
        /// <param name="storeNew">Whether or not to replace the existing GUID in persistent storage</param>
        /// <returns></returns>
        public static Guid NewGlobalGuid(bool storeNew = true)
        {
            try
            {
                //generate a new GUID
                var newGuid = Guid.NewGuid();

                //should we replace the existing GUID?
                if (storeNew)
                {
                    //store the new GUID encrypted via WDPAPI
                    var handler = new ProtectedFile(GuidFileLocation);
                    handler.WriteAllText(newGuid.ToString());
                }

                //return the newly generated GUID
                return(newGuid);
            }
            catch
            {
                //ignore all errors
            }

            //default (all zeroes)
            return(Guid.Empty);
        }
コード例 #9
0
ファイル: GuidHandler.cs プロジェクト: mitchscobell/PlexDL
        /// <summary>
        /// Load the global GUID from persistent storage; returns Guid.Empty on failure.
        /// </summary>
        /// <returns></returns>
        public static Guid StoredGlobalGuid()
        {
            try
            {
                if (File.Exists(GuidFileLocation))
                {
                    //decrypt and read the GUID from persistent storage
                    var handler    = new ProtectedFile(GuidFileLocation);
                    var guidString = handler.ReadAllText();

                    //is it a valid string?
                    if (!string.IsNullOrEmpty(guidString))
                    {
                        //validate the GUID and return the GUID struct if successful
                        if (Guid.TryParse(guidString, out var r))
                        {
                            return(r);
                        }
                    }
                }
            }
            catch
            {
                //ignore all errors
            }

            //default (all zeroes)
            return(Guid.Empty);
        }
コード例 #10
0
        public void SaveEnvironment(AzureEnvironments azureEnvironment)
        {
            var json = new JObject();

            json["ver"] = Constants.FileVersion.Value;
            json["env"] = azureEnvironment.ToString();
            File.WriteAllText(ProtectedFile.GetCacheFile(_fileName), json.ToString());
        }
コード例 #11
0
        public void ClearSavedEnvironment()
        {
            var filePath = ProtectedFile.GetCacheFile(_fileName);

            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
        }
コード例 #12
0
        public TokenCacheInfo GetRecentToken(string resource)
        {
            var file = ProtectedFile.GetCacheFile(resource == Constants.CSMResource ? _recentARMFileName : _recentAADFileName);

            if (!File.Exists(file))
            {
                return(null);
            }

            return(JsonConvert.DeserializeObject <TokenCacheInfo>(ProtectedFile.ReadAllText(file)));
        }
コード例 #13
0
        public Dictionary <string, TenantCacheInfo> GetCache()
        {
            var file = ProtectedFile.GetCacheFile(_fileName);

            if (!File.Exists(file))
            {
                return(new Dictionary <string, TenantCacheInfo>());
            }

            return(JsonConvert.DeserializeObject <Dictionary <string, TenantCacheInfo> >(ProtectedFile.ReadAllText(file)));
        }
コード例 #14
0
        public AzureEnvironments GetSavedEnvironment()
        {
            var file = ProtectedFile.GetCacheFile(_fileName);

            if (File.Exists(file))
            {
                return((AzureEnvironments)Enum.Parse(typeof(AzureEnvironments), File.ReadAllText(file)));
            }

            return(AzureEnvironments.Prod);
        }
コード例 #15
0
        public AzureEnvironments GetSavedEnvironment()
        {
            var file = ProtectedFile.GetCacheFile(_fileName);

            if (File.Exists(file))
            {
                var json = JObject.Parse(File.ReadAllText(file));
                return((AzureEnvironments)Enum.Parse(typeof(AzureEnvironments), json.Value <string>("env")));
            }

            return(AzureEnvironments.Prod);
        }
コード例 #16
0
        public string GetSavedEnvironment()
        {
            var file = ProtectedFile.GetCacheFile(_fileName);

            if (File.Exists(file))
            {
                var json = JObject.Parse(File.ReadAllText(file));
                return(json.Value <string>("env"));
            }

            return(Utils.GetDefaultEnv());
        }
コード例 #17
0
        public CustomTokenCache GetCache()
        {
            var file = ProtectedFile.GetCacheFile(_cacheFileName);

            if (!File.Exists(file))
            {
                return(new CustomTokenCache());
            }

            var state = ProtectedFile.ReadAllText(file);

            return(new CustomTokenCache(state));
        }
コード例 #18
0
        public static string DecryptSettings()
        {
            try
            {
                var protectedFile = new ProtectedFile(SettingsFile);
                return(protectedFile.ReadAllText());
            }
            catch (Exception ex)
            {
                LoggingHelpers.RecordException(ex.Message, @"SettingsDecryptionError");
            }

            //default
            return(@"");
        }
コード例 #19
0
        public static string StoredToken()
        {
            try
            {
                if (!IsTokenStored || !TokenCachingEnabled)
                {
                    return(string.Empty);
                }

                var protectedToken = new ProtectedFile(Final);
                var t = protectedToken.ReadAllText();
                return(t.Length == 20 && !string.IsNullOrEmpty(t) ? t : string.Empty); //valid Plex tokens are always 20 characters in length.
            }
            catch (Exception)
            {
                //ignore any errors
            }

            //default
            return(string.Empty);
        }
コード例 #20
0
        public static bool SaveToken(string token, bool deleteIfPresent = true)
        {
            if (deleteIfPresent)
            {
                ClearStored();
            }

            try
            {
                var protectedToken = new ProtectedFile(Final);
                if (TokenCachingEnabled)
                {
                    protectedToken.WriteAllText(token);
                }
                return(true);
            }
            catch (Exception)
            {
                //ignore any errors
            }

            //default
            return(false);
        }
コード例 #21
0
        public void SaveCache(Dictionary <string, TenantCacheInfo> tenants)
        {
            var json = JObject.FromObject(tenants);

            ProtectedFile.WriteAllText(ProtectedFile.GetCacheFile(_fileName), json.ToString());
        }
コード例 #22
0
 public void SaveEnvironment(AzureEnvironments azureEnvironment)
 {
     File.WriteAllText(ProtectedFile.GetCacheFile(_fileName), azureEnvironment.ToString());
 }
コード例 #23
0
        public void SaveCache(CustomTokenCache cache)
        {
            var state = cache.GetState();

            ProtectedFile.WriteAllText(ProtectedFile.GetCacheFile(_cacheFileName), state);
        }
コード例 #24
0
        public bool IsCacheValid()
        {
            var file = ProtectedFile.GetCacheFile(_fileName);

            return(File.Exists(file));
        }