/// <summary> /// Tries to write -> read -> clear a secret from the underlying persistence mechanism /// </summary> public void VerifyPersistence() { // do not use the _cacheAccessor for writing dummy data, as it might overwrite an actual token cache var persitenceValidatationAccessor = CacheAccessor.CreateForPersistenceValidation(); try { _logger.LogInformation($"[Verify Persistence] Writing Data "); persitenceValidatationAccessor.Write(Encoding.UTF8.GetBytes(PersistenceValidationDummyData)); _logger.LogInformation($"[Verify Persistence] Reading Data "); var data = persitenceValidatationAccessor.Read(); if (data == null || data.Length == 0) { throw new MsalCachePersistenceException( "Persistence check failed. Data was written but it could not be read. " + "Possible cause: on Linux, LibSecret is installed but D-Bus isn't running because it cannot be started over SSH."); } string dataRead = Encoding.UTF8.GetString(data); if (!string.Equals(PersistenceValidationDummyData, dataRead, StringComparison.Ordinal)) { throw new MsalCachePersistenceException( $"Persistence check failed. Data written {PersistenceValidationDummyData} is different from data read {dataRead}"); } } catch (InteropException e) { throw new MsalCachePersistenceException( $"Persistence check failed. Reason: {e.Message}. OS error code {e.ErrorCode}.", e); } catch (Exception ex) when(!(ex is MsalCachePersistenceException)) { throw new MsalCachePersistenceException("Persistence check failed. Inspect inner exception for details", ex); } finally { try { _logger.LogInformation($"[Verify Persistence] Clearing data"); persitenceValidatationAccessor.Clear(); } catch (Exception e) { _logger.LogError($"[Verify Persistence] Could not clear the test data: " + e); } } }