public async Task <string> AuthenticateTokenAsync( string url, string userName, string password) { Uri serviceAccountTokenUri = new Uri(url); AuthenticateModel request = new AuthenticateModel() { Username = userName, Password = password }; string requestJson = JsonConvert.SerializeObject(request); using (var webClient = new WebClient()) { //_logger.LogDebug($"Request:\n{requestJson}"); try { webClient.Headers.Add(HttpRequestHeader.ContentType, "application/json"); string response = await webClient.UploadStringTaskAsync(serviceAccountTokenUri, requestJson); //_logger.LogDebug($"Response:\n{response}"); var responseTokenInfo = JsonConvert.DeserializeObject <UserInfo>(response); return(responseTokenInfo.Token); } catch (Exception ex) { //_logger.LogCritical(exception.ToString()); } finally { MemoryUtilities.CleanupWebClient(webClient); } } return(null); }
public async Task <string> CallTask(string url, string token, string data, string cmdType) { Console.WriteLine(url + " - " + data); Uri serviceAccountTokenUri = new Uri(url); string requestJson = data; using (var webClient = new WebClient()) { try { ConfigureRequestHeaders(webClient, token); string response = ""; if (cmdType == "POST") { response = await webClient.UploadStringTaskAsync(serviceAccountTokenUri, requestJson); } else { response = await webClient.DownloadStringTaskAsync(serviceAccountTokenUri); } //_logger.LogDebug($"Response:\n{response}"); return(response); } catch (Exception ex) { //_logger.LogCritical(exception.ToString()); } finally { MemoryUtilities.CleanupWebClient(webClient); } } return(null); }