public async Task LoginAsync(string username, string password)
        {
            var parameters = new Dictionary <string, string> {
            };

            parameters.Add("grant_type", "password");
            parameters.Add("username", username);
            parameters.Add("password", password);
            parameters.Add("client_id", clientId);
            parameters.Add("client_secret", clientSecret);

            var response = await PostUrlEncodedContentAsync(authBaseUri, "api/Account/Login", parameters);

            if (response.IsSuccessful)
            {
                var newToken = JsonConvert.DeserializeObject <AuthTokenApiModel>(response.Content);
                token = newToken;

                if (persistingService != null)
                {
                    await persistingService.StoreAsync(token);
                }
            }
            else
            {
                throw new UnauthorizedAccessException(response.Content);
            }
        }
        private async Task RefreshTokenAsync()
        {
            if (token == null)
            {
                throw new UnauthorizedAccessException();
            }

            var parameters = new Dictionary <string, string> {
            };

            parameters.Add("grant_type", "refresh_token");
            parameters.Add("refresh_token", token.RefreshToken);
            parameters.Add("client_id", clientId);
            parameters.Add("client_secret", clientSecret);

            var response = await PostUrlEncodedContentAsync(authBaseUri, "api/Account/Login", parameters);

            if (response.IsSuccessful)
            {
                token = JsonConvert.DeserializeObject <AuthTokenApiModel>(response.Content);

                if (persistingService != null)
                {
                    await persistingService.StoreAsync(token);
                }
            }
            else
            {
                token = null;
                throw new UnauthorizedAccessException(response.Content);
            }
        }
        public ApiAccess(string authBaseUri, string baseUri, string clientId, string clientSecret, ITokenPersistingService persistingService)
        {
            this.authBaseUri  = authBaseUri;
            this.baseUri      = baseUri;
            this.clientId     = clientId;
            this.clientSecret = clientSecret;

            this.persistingService = persistingService;

            token = persistingService?.LoadAsync()?.Result;
        }
 public async Task StoreAsync(AuthTokenApiModel authToken)
 {
     try
     {
         var json    = JsonConvert.SerializeObject(authToken);
         var encoded = Convert.ToBase64String(Encoding.Unicode.GetBytes(json));
         using (FileStream fs = new FileStream(path, FileMode.Create))
             using (StreamWriter sw = new StreamWriter(fs))
             {
                 await sw.WriteAsync(encoded);
             }
     }
     catch (Exception)
     {
         throw;
     }
 }