コード例 #1
0
        public async Task DesktopStorageWriteReadDeleteFile()
        {
            // Arrange
            IStorage          storage   = new FileSystemStorage(this._storagePath);
            IStorageContainer container = await storage.GetContainerAsync(DesktopStorageContainerTests._precreatedContainerName);

            const string text     = "This is R/W test";
            const string fileName = "rw.txt";

            var memStream = new MemoryStream();
            var writer    = new StreamWriter(memStream);

            writer.Write(text);
            writer.Flush();
            memStream.Position = 0;

            string result;

            // Act
            await container.WriteFileAsync(memStream, fileName);

            using (Stream stream = await container.GetFileStreamAsync(fileName)) {
                var buffer = new byte[stream.Length];
                await stream.ReadAsync(buffer, 0, (int)stream.Length);

                result = Encoding.UTF8.GetString(buffer);
            }

            await container.DeleteFileAsync(fileName);

            var files = await container.GetFilesAsync("", StorageSearchOption.Default);

            // Assert
            Assert.NotNull(result);
            Assert.Equal(text, result);
            Assert.False(files.Any(d => string.Equals(d, fileName, StringComparison.OrdinalIgnoreCase)));
        }
コード例 #2
0
        public async Task RequestJwtAsync(UserAuthenticationModel userData, bool forceRefresh)
        {
            var fileExists = await _keyStorageContainer.PublicKeyExists(_deviceId);

            if (forceRefresh || !fileExists || fileExists && string.IsNullOrEmpty(await _storageContainer.ReadFileAsStringAsync(_jwtFilePath)))
            {
                var key         = _cryptoService.RetrieveMergedKey("server");
                var cryptedData = await _cryptoService.EncryptTripleDESAsync(JsonConvert.SerializeObject(userData), key);

                var jwtRequest = new SecureAuthenticationModel()
                {
                    Id      = _deviceId,
                    Content = Convert.ToBase64String(cryptedData)
                };

                var content = new StringContent(JsonConvert.SerializeObject(jwtRequest), Encoding.UTF8, "application/json");

                var response = await _httpClient.PostAsync("api/jwt/requestjwt", content);

                if (response.IsSuccessStatusCode)
                {
                    var responseString = await response.Content.ReadAsStringAsync();

                    var responseModel = JsonConvert.DeserializeObject <SecureJwtModel>(responseString);
                    await _storageContainer.WriteFileAsync(_jwtFilePath, JsonConvert.SerializeObject(responseModel.TokenModel));
                }
                else if (response.StatusCode == HttpStatusCode.Unauthorized)
                {
                    throw new UnauthorizedAccessException();
                }
                else
                {
                    await _storageContainer.WriteFileAsync(_jwtFilePath, "");

                    throw new Exception(response.ReasonPhrase);
                }
            }
        }
コード例 #3
0
 public async Task WritePrivateKeyAsync(int id, string value)
 {
     await _storageContainer.WriteFileAsync($"{id}/key.priv", value);
 }