using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Text; using System.Threading.Tasks; using System.Web; using System.Web.Script.Serialization; namespace ARC.GMP.Utils { public class TokenManager { public async Task GetToken(string baseAddress, string clientId, string userName, string Password, string userDomain, string userId) { string result = ""; ServicePointManager.DefaultConnectionLimit = 10; HttpClient client = null; HttpResponseMessage response = null; try { using (client = new HttpClient()) { client.BaseAddress = new Uri(baseAddress); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpContent tokenContent = new FormUrlEncodedContent( new Dictionary { { "grant_type", "password" }, { "client_Id", clientId }, { "username", userName }, { "password", Password }, { "logon_userdomain", userDomain }, { "logon_userid", userId } }); // New code: using (response = await client.PostAsync("Token", tokenContent)) { if (response.IsSuccessStatusCode) { var byteResponse = await response.Content.ReadAsByteArrayAsync(); JObject obj = JObject.Parse(Encoding.UTF8.GetString(byteResponse)); result = (string)obj["access_token"]; response.Dispose(); } else { result = ""; } } } } catch (Exception ex) { result = "{\"Status\":\"Timedout\",\"StatusDescription\":" + ex.Message + "}"; } finally { if (client != null) client.Dispose(); if (response != null) response.Dispose(); } return result; } } }