/// <summary> /// Caches an SSO Token to disk. /// </summary> /// <param name="token">SSO Token to cache</param> public bool TrySave(SsoToken token) { try { if (string.IsNullOrWhiteSpace(_cachePath)) { return(false); } if (token.StartUrl != StartUrl) { throw new ArgumentException("StartUrl mismatch between cache and token", nameof(token)); } var json = SsoTokenUtils.ToJson(token); Directory.CreateDirectory(Path.GetDirectoryName(_cachePath)); File.WriteAllText(_cachePath, json); return(true); } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception e) #pragma warning restore CA1031 // Do not catch general exception types { _logger.Error(e, "Warning: Unable to save SSO Token Cache. Future retrieval will have to produce a token."); return(false); } }
/// <summary> /// Loads an SSO Token from cache /// </summary> /// <remarks> /// This method is private. The spec prohibits storing Tokens in memory. They are to be accessed to from disk /// when necessary. /// </remarks> /// <returns>Loaded SSO Token. Returns null if no cache exists, or is otherwise corrupt.</returns> private SsoToken Load() { try { if (string.IsNullOrWhiteSpace(_cachePath) || !File.Exists(_cachePath)) { _logger.InfoFormat("No SSO Token cache to load from"); return(null); } var json = File.ReadAllText(_cachePath); var ssoToken = SsoTokenUtils.FromJson(json); _logger.InfoFormat("SSO Token loaded from cache"); return(ssoToken); } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception e) #pragma warning restore CA1031 // Do not catch general exception types { _logger.Error(e, "Unable to load token cache for start url: {0}", StartUrl); return(null); } }