Пример #1
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);
                }
            }
        }
 public async Task <string> ReadPrivateKeyAsStringAsync(int id)
 {
     return(await _storageContainer.ReadFileAsStringAsync($"{id}/key.priv"));
 }